JavaScript Number.isNaN() Method
The Number.isNaN() method is the built-in method of JavaScript Number library. You can invoke it by taking the reference of it. The Number.isNaN() method checks whether the value is NaN (Not a Number) and whether the value’s data type is a number or not.
Syntax
let integer = 202;
Number.isNaN(integer);
Parameters
value |
The Number.isNaN() checks for the value whether it is a NaN and its type is a number or not. |
Returns
It returns the true (a Boolean value) if the specified value is a NaN and its type is number; otherwise, it returns a false.
Example Codes
Let’s learn the Number.isNaN() method using different code snippets below.
Pass Different Numeric to Number.isNaN() Method
The following code takes some NaN and numeric values to observe the output of the Number.isNaN() method. The below output shows that the method returns true for the NaN and Infinity values as its type is a number, but it is not a numeric value.
let notNumber1 = Number.NaN;
let notNumber2 = 0/0;
let number = 30;
console.log(Number.isNaN(notNumber1));
console.log(Number.isNaN(notNumber2));
console.log(Number.isNaN(Number));
Output:
true
true
false
Use the Number.isNaN() Method With Non-Numeric Values
The Number.isNaN() method is the updated version of the isNaN() method. The isNaN() method only checks whether the given value is a NaN or not and doesn’t check for the type of value.
The Number.isNaN() method also checks whether the value’s data type is number or not. If the type of value is not a number, it always returns false, which you can see in the example below as we have passed the string values as an argument of the Number.isNaN() method.
let value1 = "DelftStack";
let value2 = [10];
console.log(Number.isNaN(value1));
console.log(Number.isNaN(value2));
Output:
false
false
In the above example codes, we have seen that if we pass the non-numeric value as an argument of the Number.isNaN() method, it always returns the false, so you should use this method only for the numbers.
