JavaScript parseFloat() | Get the First Floating-point Value from a Specified Value

The JavaScript parseFloat() method is used to get the first floating-point (or integer) value from the specified value, if the specified value starts with a number. For example:

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

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

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

Please note: To get only the first integer, use parseInt(). parseInt("12.43") returns 12, whereas parseFloat("12.43") returns 12.43.

JavaScript parseFloat() Syntax

The syntax of parseFloat() method in JavaScript is:

parseFloat(x)

where x refers to a value from which the first integer or floating-point value will be returned.

The parseFloat() method returns a number which will be the starting floatint-point number of the specified value. Otherwise returns NaN. For example:

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

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

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

parseFloat(40.578) =

parseFloat("40.578") =

parseFloat("42.55") =

parseFloat("12 43") =

parseFloat(" 43") =

parseFloat("18 September") =

parseFloat("September 18") =

parseFloat("-45.578") =

JavaScript Online Test


« Previous Tutorial Next Tutorial »