JavaScript | parseInt() with Examples

The parseInt() is an inbuilt function in JavaScript which is used to accept the string and convert it into an integer. It actually returns an integer of the specified radix(base) which is accepted as second parameter of the parseInt() function. If the string does not contain a numeric value then it returns NaN i.e, not a number.
Syntax:

parseInt(Value, radix)

Parameters: It accepts two parameter which are specified below-

  • Value: “Value” 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 which is not a number in the specified radix(base).
    Example:



    Input: var n = parseInt("2018@geeksforgeeks");
    Output: n = 2018
    now n contains 2018 as '@' is not a Number 
    and parsing stops at that point,further characters are ignored.
    Input: var a = parseInt("1000");
    Output: a = 1000(Number)
    

    JavaScript to show the working of parseInt() function:

    Code #1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

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

    Code #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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
        <body>
            <script>
              
            //base 10
            a = parseInt("100",10);
            document.write('parseInt("100",10) = ' + a + "<br>");
              
            //base 8
            b = parseInt("8",8);
            document.write('parseInt("8",8) = ' + b + "<br>");
              
            //base 8
            c = parseInt("15",8);
            document.write('parseInt("15",8) = ' + c + "<br>");
              
            //base 16
            d = parseInt("16",16);
            document.write('parseInt("16",16) = ' + d + "<br>");
              
            // Leading and trailing spaces are ignored in parseInt() function
            e = parseInt(" 100 ");
            document.write('parseInt(" 100 ") = ' + e + "<br>");
              
            //base 16(hexadecimal)
            f = parseInt("0x16");
            document.write('parseInt("0x16") = ' + f + "<br>");
              
            </script>
          
        </body>
    </html>                                

    chevron_right

    
    

    Output:

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


    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.




    Article Tags :

    1


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