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

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 29-Jan-2018 14:41:23
Advertisements