How do I convert a string into an integer in JavaScript?


The parseInt() method in JavaScript converts a string into a number. The method returns NaN if the value entered cannot be converted to a number.

Example

You can try to run the following code to convert a string into an integer −

 Live Demo

<!DOCTYPE html>
<html>
   <body>
      <button onclick = "display()">Check</button>
      <p id = "demo"></p>

      <script>
         function display() {
            var val1 = parseInt("50") + "<br>";
            var val2 = parseInt("52.40") + "<br>";
            var val3 = parseInt(" 75 ") + "<br>";

            var res = val1 + val2 + val3;
            document.getElementById("demo").innerHTML = res;
         }
      </script>
   </body>

</html>

Updated on: 07-Jan-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements