JavaScript parseInt() | Get the First Integer Value from a Specified Value

The JavaScript parseInt() method is used to get the integer (starting/first integer) from the specified value. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="res"></p>

   <script>
      document.getElementById("res").innerHTML = parseInt("19 Sep");
   </script>
   
</body>
</html>
Output

JavaScript parseInt() Syntax

The syntax of parseInt() method in JavaScript is:

parseInt(x, radix)

where x parameter is required, refers to a value, from which the first integer value will be returned. And the radix parameter is optional, used when we need to convert the integer value to particular number system. List of values we can use as radix are:

The parseInt() method return a number which will be the starting integer part of the specified value or string (most of the time). Otherwise return NaN. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p>parseInt(40) =  <span id="a"></span></p>
   <p>parseInt("40") =  <span id="b"></span></p>
   <p>parseInt("42.55") =  <span id="c"></span></p>
   <p>parseInt("12 43") =  <span id="d"></span></p>
   <p>parseInt(" 43") =  <span id="e"></span></p>
   <p>parseInt("18 September") =  <span id="f"></span></p>
   <p>parseInt("September 18") =  <span id="g"></span></p>
   <p>parseInt("-45") =  <span id="h"></span></p>

   <script>
      document.getElementById("a").innerHTML = parseInt(40);
      document.getElementById("b").innerHTML = parseInt("40");
      document.getElementById("c").innerHTML = parseInt("42.55");
      document.getElementById("d").innerHTML = parseInt("12 43");
      document.getElementById("e").innerHTML = parseInt(" 43");
      document.getElementById("f").innerHTML = parseInt("18 September");
      document.getElementById("g").innerHTML = parseInt("September 18");
      document.getElementById("h").innerHTML = parseInt("-45");
   </script>
   
</body>
</html>
Output

parseInt(40) =

parseInt("40") =

parseInt("42.55") =

parseInt("12 43") =

parseInt(" 43") =

parseInt("18 September") =

parseInt("September 18") =

parseInt("-45") =

JavaScript Online Test


« Previous Tutorial Next Tutorial »