How to convert a value to a number in JavaScript?


In this article we are going to discuss how to convert a value to a number in JavaScript.

JavaScript has introduced Number(), parseInt(), parseFloat() methods to convert a value into a number and also we can achieve it by using (+) unary operator. These mentioned methods can convert number strings to numbers and Boolean values to 1's or 0's. Let’s us discuss these above mentioned methods briefly.

We’ll discuss the 4 possible ways to convert a value to a number with an example each.

  • Number()

  • parseInt()

  • parseFloat()

  • Unary Opertaor(+)

Example 1

The following is an example program to convert a value to a number using Number() method.

<!DOCTYPE html>
<html>
<head>
   <title>To convert a value to a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3></h3>
   <p id='valToNum'>To convert a value to a number in JavaScript using Number()</p>
   <script>
      let a = Number("100");
      let b = Number("100.234");
      let c = Number(true); //return 0 or 1 for boolean
      let d = Number("true"); //returns a number or NaN
      let e = Number(new Date()); //returns number of milli seconds from Jan 1 1970
      let f = Number([1.2]);
      document.getElementById('valToNum').innerHTML = `Number("100") is ${a} ${'<br/>'} Number("100.234") is ${b} ${'<br/>'} Number(true) is ${c} ${'<br/>'} Number("true") is ${d} ${'<br/>'} Number(new Date()) is ${e} ${'<br/>'} Number([1.2]) is ${f} `;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 2

The following is an example program to convert a value to a number using parseInt() method.

<!DOCTYPE html>
<html>
<head>
   <title>To convert a value to a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3></h3>
   <p id='valToNum'>To convert a value to a number in JavaScript using parseInt()</p>
   <script>
      //ParseInt() parses a string and returns a whole number, else NaN.
      let a = parseInt("100");
      let b = parseInt("100.234");
      let c = parseInt([1.2]);
      let d = parseInt(true);
      document.getElementById('valToNum').innerHTML = `parseInt("100") is ${a} ${'<br/>'} parseInt("100.234") is ${b} ${'<br/>'} parseInt([1.2]) is ${c} ${'<br/>'} parseInt(true) is ${d} `;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 3

The following is an example program to convert a value to a number using parseFloat() method.

<!DOCTYPE html>
<html>
<head>
   <title>To convert a value to a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>To convert a value to a number in JavaScript using parseFloat()</h3>
   <p id='valToNum'></p>
   <script>
      //ParseFloat() parses a string and returns a Number.
      let a = parseFloat("100");
      let b = parseFloat("100.234");
      let c = parseFloat([1.2]);
      let d = parseFloat(true);
      document.getElementById('valToNum').innerHTML = `parseFloat("100") is ${a} ${'<br/>'} parseFloat("100.234") is ${b} ${'<br/>'} parseFloat([1.2]) is ${c} ${'<br/>'} parseFloat(true) is ${d} `;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 4

The following is an example program to convert a value to a number using unary operator(+).

<!DOCTYPE html>
<html>
<head>
   <title>To convert a value to a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>To convert a value to a number in JavaScript using Unary operator</h3>
   <p id='valToNum'></p>
   <script>
      // Unary operator converts a string to a Number.
      let a = +"100";
      let b = +"-100.234";
      let c = +[1.2];
      document.getElementById('valToNum').innerHTML = `+"100" is ${a} ${'<br/>'} +"-100.234" is ${b} ${'<br/>'} +[1.2] is ${c} ${'<br/>'} `;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Updated on: 08-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements