Search This Blog

Monday, 1 August 2016

How to check whether a number is NaN in javascript?

Html page:

<button onclick="myFunction()">Try it</button>
<p id="P7"></p>

Javascript page:

<script>
    function myFunction() {
        var res = "";
        res = res + Number.isNaN(123) + ": 123<br>";
        res = res + Number.isNaN(-1.23) + ": -1.23<br>";
        res = res + Number.isNaN(5 - 2) + ": 5-2<br>";
        res = res + Number.isNaN(0) + ": 0<br>";


        document.getElementById("demo").innerHTML = res;
    }
</script>

Output:

false: 123
false: -1.23
false: 5-2

false: 0

No comments:

Post a Comment