Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript parseInt() Function

Improve Article
Save Article
  • Last Updated : 16 Jan, 2023
Improve Article
Save Article

The JavaScript parseInt() Function is used to accept the string and 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. 

Note:
parseInt(): These functions were used to convert strings to numbers, but they are now considered deprecated in favor of the Number() and parseInt() functions, which provide more options and better support for different number formats.

Syntax:

parseInt(Value, radix)

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

  • Value: This parameter contains a string that 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 is an example of the parseInt() function.

Example: 

javascript




<script>
    var v1 = parseInt("3.14");
     
    console.log('Using parseInt("3.14") = '
    + v1);
</script>

Output:

Using parseInt("3.14") = 3

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: 

javascript




<script>
    a = parseInt("100");
    console.log('parseInt("100") = ' +
    a);
     
    // It returns a Integer until
    // it encounters Not a Number character
    b = parseInt("2018@geeksforgeeks");
    console.log('parseInt("2018@geeksforgeeks") = ' +
    b);
     
    // It returns NaN on Non numeral character
    c = parseInt("geeksforgeeks@2018");
    console.log('parseInt("geeksforgeeks@2018") = ' +
    c);
     
    // It returns Integer value of a Floating point Number
    d = parseInt("3.14");
    console.log('parseInt("3.14") = ' + d);
     
    // It returns only first Number it encounters
    e = parseInt("21 7 2018");
    console.log('parseInt("21 7 2018") = ' +
    e);
</script>

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 the parseInt() function and the start of the string contains “0x” then it is treated as a hexadecimal value. By default, the radix is 10 (decimal). Note that in line 11 there is ‘8’ which is a character that is not defined in the radix 8 numeral system therefore it returns NaN. 

javascript




<script>
    // Base 10
    a = parseInt("100",10);
    console.log('parseInt("100",10) = ' +
    a);
     
    // Base 8
    b = parseInt("8",8);
    console.log('parseInt("8",8) = ' +
    b);
     
    // Base 8
    c = parseInt("15",8);
    console.log('parseInt("15",8) = ' +
    c);
     
    // Base 16
    d = parseInt("16",16);
    console.log('parseInt("16",16) = ' +
    d);
     
    // Leading and trailing spaces are ignored
    // in parseInt() function
    e = parseInt(" 100 ");
    console.log('parseInt(" 100 ") = ' +
    e);
     
    // Base 16(hexadecimal)
    f = parseInt("0x16");
    console.log('parseInt("0x16") = ' +
    f);
     
</script>

Output:

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

We have a complete list of Javascript Function methods, to check those please go through this Javascript Function Complete reference article.

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Safari 1 and above
  • Opera 3 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!