Skip to content

JavaScript parseint function| Convert a string to an integer Example

  • by

The javaScript parseint() method is to convert a string to an integer (Number). The parseInt() is an inbuilt function in JavaScript, it accepts the string, radix parameter, and converts it into an integer. If the given string doesn’t contain numbers, it returns Nan( not a number)


parseInt(string, radix)

Parameter Values

  • string: A given value want to parse to int.
  • radix: Its and Optional. An integer between 2 and 36 represents the radix (the base in mathematical numeral systems) of the string.

Return value

An integer parsed from the given string.

Examples of JavaScript parseint() function

Let’s see the example with different type of input.

Parse different strings:

<!DOCTYPE html> 
        a = parseInt("100"); 
        document.write('parseInt("100") = ' + a + "<br>"); 
        // It returns a Integer until 
        // it encounters Not a Number character 
        b = parseInt("2020@Covid19"); 
        document.write('parseInt("2020@Covid19") = ' + b + "<br>"); 
        // It returns NaN on Non numeral character 
        c = parseInt("Covid19"); 
        document.write('parseInt("Covid19") = ' + 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 2020") = ' + e + "<br>"); 


JavaScript parseint function

Parseint negative number javascript

var x = parseInt("-2147483648");

Q: How to convert a string into an integer in JavaScript?

Answer: You can converts string and into an integer using a parseInt() function.

 var a = "10";
 var b = parseInt(a);

Read more examples and methods:-

Do comment if you have any doubts and suggestions on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *