JavaScript Convert a String to Integer Number

Covert string to integer in JavaScript; Through this tutorial, i am going to show you how to convert string to integer in JavaScript.

Convert a string to an integer number in JavaScript

Using the javaScript parseInt() function, you can convert string to integer number; as follows:

  • JavaScript parseInt() Function
  • JavaScript parseInt() Function Syntax
  • Examples of Convert a String to Integer Number in javascript

JavaScript parseInt() Function

The javascript parseInt () function returns an integer number by parse the string. If the first character can not be changed in number, the function returns NaN. JavaScript parseInt simple convert a string to an integer number

JavaScript parseInt() Function Syntax

The syntax of javascript parseInt () is very simple. The JavaScript parseInt () should look like this :

parseInt(string, radix)
  • string Needed. The string you want to be parsed
  • radix Not required. The numeral system representing a number.

Example 1 – Convert a String to Integer Number in javascript

Let’s take an example to convert string to integer number in JavaScript using parseInt() method; as follows:

<script>
    // returns a Integer no
    a = parseInt("20");
    document.write('parseInt("20") = ' + a + "<br>");
    //  Not a Number character
    b = parseInt("30.00");
    document.write('parseInt("30.00") = ' + b + "<br>");
    // returns NaN on Non numeral character
    c = parseInt("2.366");
    document.write('parseInt("2.366") = ' + c + "<br>");
    // returns Integer value of a Floating point Number
    d = parseInt("3.14");
    document.write('parseInt("3.14") = ' + d + "<br>");
    // only first Number it encounters
    e = parseInt("22 3 2019");
    document.write('parseInt("22 3 2019") = ' + e + "<br>");
</script>

Output

 
parseInt("20") = 20
parseInt("30.00") = 30
parseInt("2.366") = 2
parseInt("3.14") = 3
parseInt("22 3 2019") = 22

More JavaScript Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.