# JavaScript parseInt() Function

Below is the example of the parseInt() function.

• Example:

 `  `  `                    `

• Output:
`Using parseInt("3.14") = 3`

The parseInt() function is used to accept the string ,radix parameter and convert it into an integer.The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number. If the string does not contain a numeric value then it returns NaN i.e, not a number.

Syntax:

`parseInt(Value, radix)`

Parameters: This function accepts two parameters as mentioned above and described below:

• Value: This parameter contains a string which is converted to an integer.
• radix: This parameter represents the radix or base to be used and it is optional.
• Return value: It returns a number and if the first character can’t be converted to a number then the function returns NaN. It actually returns a number parsed up to that point where it encounters a character that is not a number in the specified radix(base).

Below examples illustrate the parseInt() function in JavaScript:

• Example 1: The n contains 2018 as ‘@’ is not a Number and parsing stops at that point,further characters are ignored.
```Input: var n = parseInt("2018@geeksforgeeks");
Output: n = 2018```
• Example 2:
```Input: var a = parseInt("1000");
Output: a = 1000(Number)```

More example codes for the above function are as follows:

Program 1:

 ` ` ` ` ` ` `    `` ` ` ` `                     `

Output:

```parseInt("100") = 100
parseInt("2018@geeksforgeeks") = 2018
parseInt("geeksforgeeks@2018") = NaN
parseInt("3.14") = 3
parseInt("21 7 2018") = 21```

Program 2: If the radix is not mentioned in parseInt() function and starting of string contain “0x” than it treated as hexadecimal value. By default radix is 10 (decimal). Note that in line 11 there is ‘8’ which is a character that is not defined in radix 8 numeral system therefore it returns NaN.

 ` ` ` ` `    `` ` `        `` ` `     `  `    `` ` `                                 `

Output:

```parseInt("100",10) = 100
parseInt("8",8) = NaN
parseInt("15",8) = 13
parseInt("16",16) = 22
parseInt(" 100 ") = 100
parseInt("0x16") = 22```

Supported Browsers:

• Firefox
• Internet Explorer
• Safari
• Opera

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : prateekskl99

Article Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.