JavaScript Number.isNaN()
Method
Example
Check if the parameter is Number.NaN:
Definition and Usage
NaN stands for "Not-a-Number", literally translated as not a number.
In JavaScript, NaN is an invalid number.
The Number.isNaN()
method is used to determine whether the passed value is NaN and whether its type is Number. If the value is NaN and the type is Number, it returns true; otherwise, it returns false.
In JavaScript, the most special aspect of NaN is that we cannot use the equality operators == and === to determine if a value is NaN, because both NaN == NaN and NaN === NaN will return false. Therefore, there must be a method to determine if a value is NaN.
Compared to the global function isNaN(), Number.isNaN()
does not implicitly convert the parameter to a number. It only returns true if the parameter is a number with the value NaN.
Number.isNaN(NaN); // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0) // true
// The following would return true if using the global isNaN().
Number.isNaN("NaN"); // false, the string "NaN" is not implicitly converted to the number NaN.
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN("blabla"); // false
// The following all return false
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);
Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");
Browser Support
Number.isNaN()
is a feature of ECMAScript6 (ES6).
Most modern browser versions support ES6 (JavaScript 2015).
Number.isInteger()
is not supported in Internet Explorer 11 and earlier versions.
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
Syntax
Number.isNaN(value)
Parameter Values
Parameter | Description |
---|---|
value | The value to be tested. |
Return Value
Type | Description |
---|---|
Boolean | Returns true if the value is NaN and the type is Number, otherwise returns false. |
Technical Details
| JavaScript Version: | ECMAScript 6 | | --- | --- |
More Examples
Example
Check if the parameter is an integer: