3 Example(s) of Javascript isNaN functions


Description :

NaN stands for Not a Number, so isNaN function will tell whether the passed value is not a number OR number. This is how we can pass the value in isNaN function and get the output: isNaN(NaN) , isNaN(undefined) , isNaN(null) , isNaN(37) will result in true,true,false and false.


Javascript isNaN functions Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
     
</head>
<body>
<p>The isNaN() function returns true if the value is NaN (Not-a-Number), and false if not.</p>
<button onclick="checkNumber()">Check the value</button>
<p id="sample"></p>
<script>
function checkNumber() {
    var a = isNaN(NaN) + "<br>";
    var b = isNaN(undefined) + "<br>";
    var c = isNaN(null) + "<br>";
    var d = isNaN(37) + "<br>";
     numbers=a+b+c+d;
     document.getElementById("sample").innerHTML = numbers;
}
</script>

</body>
</html>

Output

Description :

In this example, We will see isNaN with blank value. In JavaScript, isNaN("") will return false because JavaScript treat blank string as 0(zero) and "" == 0 will return true. so that is why IsNaN("") will return false.


Javascript isNaN functions Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
     
</head>
<body>
<p>The isNaN() function returns true if the value is NaN (Not-a-Number), and false if not.</p>
<button onclick="checkNumber()">Check the value</button>
<p id="sample"></p>
<script>
function checkNumber() {
    var a = isNaN("37") + "<br>";
    var b = isNaN("37.37") + "<br>";
    var c = isNaN("") + "<br>";
    var d = isNaN(" ") + "<br>";
      
    numbers=a+b+c+d;
    document.getElementById("sample").innerHTML = numbers;
}
</script>

</body>
</html>

Output

Description :

new Date instances will returns the timestamp and it won't be NaN for any valid Date. So isNaN(new Date()) will return false.


Javascript isNaN functions Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
     
</head>
<body>
<p>The isNaN() function returns true if the value is NaN (Not-a-Number), and false if not.</p>
<button onclick="checkNumber()">Check the value</button>
<p id="sample"></p>
<script>
function checkNumber() {
    var a = isNaN(new Date()) + "<br>";
    var b = isNaN(new Date().toString()) + "<br>";
    var c = isNaN("blabla") + "<br>";
    numbers=a+b+c;
    document.getElementById("sample").innerHTML = numbers;
}
</script>
</body>
</html>

Output