How to convert a string into integer in JavaScript?


In this article, we’ll go over how to convert a string into integer in JavaScript with appropriate examples.

To convert a string to an integer parseInt(), Number(), and Unary operator (+) function is used in JavaScript. The parseInt() function returns NaN ( not a number) when the string doesn’t contain number. If a string with a number is sent, then only that number will be returned as the output. This function won't accept spaces. If any particular number with spaces is sent, then the part of the number that presents before space will be returned as the output.

To convert a string into integer we can use parseInt(), Number(), and Unary operator (+). Each of them is discussed with an example.

Using the parseInt() method

The syntax for parseInt() method is −

parseInt(string);
parseInt(string,radix);

Where,

  • string is the string to be parsed.

  • radix is used to mention the base in Number System. 2 for binary, 8 for octal, 10 for decimal and 16 for hexadecimal.

This method returns the integer value that is parsed from the string.

Example

This is an example program to convert a string into an integer using parseInt() method.

<!DOCTYPE html>
<html>
<head>
   <title>To convert a string to an integer in JavaScript</title>
</head>
<body style="text-align : center">
   <h3></h3>
   <p id='strToInt'>To convert a string to an integer in JavaScript</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);
      let e = parseInt("hello")
      document.getElementById('strToInt').innerHTML = `parseInt("100") is ${a} ${'<br/>'} parseInt("100.234") is ${b} ${'<br/>'} parseInt([1.2]) is ${c} ${'<br/>'} parseInt(true) is ${d} ${'<br/>'} parseInt('hello') is ${e} `;
   </script>
</body>
</html>

Using the Number() method

The syntax for Number() method is

Number(value)

Where, value can be of any primitive data type that is convertible to number, else it returns NaN. This method returns a number.

Example

This is an example program to convert a string into an integer using Number() method.

<!DOCTYPE html>
<html>
<head>
   <title>To convert a string into integer in JavaScript</title>
</head>
<body style="text-align : center">
   <h3></h3>
   <p id='valToNum'>To convert a string into integer in JavaScript using Number()</p>
   <script>
      let a = Number("100");
      let b = parseInt(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]);
      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]) is ${f} `;
   </script>
</body>
</html>

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

Using the unary operator

The syntax for unary plus operator is −

+op;

Where, op is an operand. The unary operator + is placed before the operand and tries to convert it into a number. A number is returned.

Example

This is an example program to convert a string into an integer using Unary operator +.

<!DOCTYPE html>
<html>
<head>
   <title>To convert a string into Integer in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>To convert a string into integer 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";
      document.getElementById('valToNum').innerHTML = `+"100" is ${a} ${'<br/>'} +"-100.234" is ${b} ${'<br/>'} `;
   </script>
</body>
</html>

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

Updated on: 29-Aug-2023

207K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements