Description
The JavaScript Number method isNaN()
checks whether the given number is a NaN (nothing but Not-a-Number).
- It returns
true
if the value isNaN
and its type is aNumber
. - It is a static method, so it can be directly accessed using the class
Number
and doesn't need its instance. NaN
is considered a number but not a legal number.
Syntax
The method isNaN()
has the below syntax, where num
is a number.
Number.isNaN(num)
Parameters
The method isNaN()
allows the below parameters.
num
- It is a number to be checked for being a NaN.
Result
Returns true
if the given number is a NaN
with type Number
, otherwise returns false
.
Example 1: Using the Method
The below example shows the basic usage of the method.
// Returns true
console.log(Number.isNaN(0/0)); // Prints: true
// Returns false
console.log(Number.isNaN(123)); // Prints: false
console.log(Number.isNaN("123")); // Prints: false
console.log(Number.isNaN(123.45)); // Prints: false
console.log(Number.isNaN("Hello")); // Prints: false
console.log(Number.isNaN("2020/12/31")); // Prints: false
console.log(Number.isNaN(Infinity)); // Prints: false
console.log(Number.isNaN(-Infinity)); // Prints: false
Output:
true
false
false
false
false
false
false
false
Overall
The JavaScript Number method isNaN()
checks whether the given number is a NaN (nothing but Not-a-Number).