The parseInt() Method

The parseInt() method parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:

HTML file: Displayed by browser:
<body>
<p>The global JavaScript function parseInt() converts strings to numbers.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
    parseInt("10") + "<br>" +           // returns 10
    parseInt("10.33") + "<br>" +      // returns 10
    parseInt("10 20 30") + "<br>" + // returns 10
    parseInt("10 6") + "<br>" +        // returns 10
    parseInt("10 years") + "<br>" + // returns 10
    parseInt("years 10");                  // returns NaN
</script>
</body>

The global JavaScript function parseInt() converts strings to numbers.

If the number cannot be converted, NaN (Not a Number) is returned.

BackTable of ContentsNext