JavaScript Number isNAN() Method
The JavaScript Number isNaN() method is used to determine whether a given number is a "NaN" or not. The "NaN" stands for 'Not a Number'. If you are familiar with this acronym, you have an idea about the purpose of this method. The method returns a boolean value 'true', if the given value is not a number, and 'false' otherwise.
isNan() vs Number.isNan()
The isNaN() method returns true; if the value is not a number, whereas the Number.isNaN() returns true; if the number is not a number(NaN).
Note: The isNaN() method converts the value to the number; if neccessary.
Syntax
Following is the syntax of JavaScript Number isNaN() method −
Number.isNaN(value)
Parameters
This method accepts a parameter named 'value'. The same is described below −
value − The value to be checked.
Return value
This method returns 'true'; if the value is not a number (NaN); 'false' otherwise.
Example 1
If the passed value is a number, this method returns 'false'.
In the following example, we are passing the '1234' value as an argument to the isNaN() method to check whether it is a number or not.
<html>
<head>
<title>JavaScript isNAN() Method</title>
</head>
<body>
<script>
let value = 1234;
document.write("Given value = ", value);
document.write("<br>Value is 'NaN' or not? ", Number.isNaN(value));//return false
</script>
</body>
</html>
Output
The above program returns 'false'.
Given value = 1234 Value is 'NaN' or not? false
Example 2
Here's another example of the JavaScript Number isNaN() method. We are passing 'NaN' as an argument to this method to determine whether the value 'NaN' is Not a number. Since it is not a number, it will return true.
<html>
<head>
<title>JavaScript isNAN() Method</title>
</head>
<body>
<script>
let value = NaN;
document.write("Given value = ", value);
document.write("<br>Value is 'NaN' or not? ", Number.isNaN(value));//return true
</script>
</body>
</html>
Output
Once the above program is executed, it will return 'true'.
Given value = NaN Value is 'NaN' or not? true
Example 3
Let's compare the results of passing the same value to both 'isNaN()' and 'Number.isNaN()' methods.
<html>
<head>
<title>JavaScript isNAN() Method</title>
</head>
<body>
<script>
let value = 'Hello';
document.write("Given value = ", value);
document.write("<br>The 'isNaN()' method returns: ", isNaN(value));//returns true
document.write("<br>The 'Nunber.isNaN()' method returns: ", Number.isNaN(value));// returns false
</script>
</body>
</html>
Output
The above program produces the following output −
Given value = Hello The 'isNaN()' method returns: true The 'Nunber.isNaN()' method returns: false
Example 4
Let's see the real-time usage of this method. We will use the method's result within the conditional statement to print the inner statement based on the result produced by this method.
<html>
<head>
<title>JavaScript isNAN() Method</title>
</head>
<body>
<script>
function verify(value){
if(Number.isNaN(value)){
return true;
}
else{
return false;
}
}
let val1 = NaN;
let val2 = 122;
let val3 = 'abc';
document.write("'", val1, "' is (not a number) = ", verify(val1));
document.write("<br>'", val2, "' is (not a number) = ", verify(val2))
document.write("<br>'", val3, "' is (not a number) = ", verify(val3));
</script>
</body>
</html>
Output
After executing the above program, it displays the following statements −
'NaN' is (not a number) = true '122' is (not a number) = false 'abc' is (not a number) = false