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

The JavaScript parseInt() method is used to get the integer (starting or 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 the 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; it is used when we need to convert the integer value to a particular number system. List of values we can use as radix are:

The parseInt() method returns a number that 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 »


Liked this post? Share it!