Converting Strings to Numbers

The following table list all three functions we can use to convert string to a number:

MethodDescription
Number(<str>) Parses the string to create an integer or real value
parseInt(<str>) Parses the string to create an integer value
parseFloat(<str>) Parses the string to create an integer or real value
 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
  <script type="text/javascript">
    var firstVal = "5";
    var secondVal = "5";
    var result = Number(firstVal) + Number(secondVal);
    document.writeln("Result: " + result);
  </script>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Essential Types  

Number:
  1. The Number Type
  2. Number toExponential()
  3. Number toFixed()
  4. Number toPrecision()
  5. Number toString()
  6. Number valueOf()
  7. Converting Strings to Numbers
  8. typeof and instanceof for Number objects and primitive values