Javascript Reference - JavaScript isNaN() Function








The isNaN() function determines whether a value is an illegal number (NaN).

This function returns true if the value is NaN, and false if not.

Browser Support

isNaN Yes Yes Yes Yes Yes

Syntax

isNaN(value)

Parameter Values

Parameter Description
value Required. The value to be tested




Return Value

A Boolean. Returns true if the value is NaN, otherwise it returns false.

Example

The following code shows how to check if a string is a Not-A-Number with isNaN function.


console.log(isNaN("10"));
//false - can be converted to number 10
/*from  w  w  w.  ja va  2  s.  c om*/
var entry1 = "123.123";
var number = new Number();
number = Number(entry1);
if (isNaN(number)){
    console.log("You did not enter a valid number.");
}else{
    console.log(number.valueOf());
}

The code above generates the following result.





Example 2

The following code shows how to check whether a number is an illegal number.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--  www  .java  2s.  c  o  m-->
<p id="demo"></p>

<script>
function myFunction() {
    var a = isNaN(123) + "<br>";
    var b = isNaN(-1.23) + "<br>";
    var c = isNaN(5-2) + "<br>";
    var d = isNaN(0) + "<br>";
    var e = isNaN("Hello") + "<br>";
    var f = isNaN("2014/12/12") + "<br>";

    var res = a + b + c + d + e + f;
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

The code above is rendered as follows: