JavaScript Number.isNaN() Method
Example
Check whether the parameter is Number.NaN:
Number
.
isNaN
(
123
)
;
Number
.
isNaN
(
1.23
)
;
Number
.
isNaN
(
'
123
'
)
;
Number
.
isNaN
(
0
/
0
)
;
Number
.
isNaN
(
5
2
)
;
Number
.
isNaN
(
0
)
;
Number
.
isNaN
(
'
Hello
'
)
;
Number
.
isNaN
(
'
2005/12/12
'
)
;
Number
.
isNaN
(
'
'
)
;
Try it »
Definition and Usage
NaN is the abbreviation of "Not-a-Number", literally translated asNot a number.
In JavaScript, NaN is an illegal number.
The Number.isNaN() method is used to determine whether the passed value is NaN and to check whether its type is Number. If the value is NaN and its type is Number, it returns true; otherwise it returns false.
In JavaScript, the most special thing about NaN is that we cannot use the equality operators == and === to determine whether a value is NaN, because both NaN == NaN and NaN === NaN return false. Therefore, there must be a method to determine whether a value is NaN.
Compared with the global function isNaN() Number.isNaN() does not automatically convert its argument to a number; it returns true only when the argument is a number with the value NaN.
Number.isNaN(NaN); // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0) // true
// If these are used with the global isNaN(), they will return true.
Number.isNaN("NaN"); // false,字符串 "NaN" 不会被隐式转换成数字 NaN。
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN("blabla"); // false
// All of the following 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 new feature of ECMAScript 6 (ES6).
Basically, the latest browser versions support ES6 (JavaScript 2015).
Number.isInteger() is not supported in Internet Explorer 11 and earlier browser versions.
| Chrome | Edge | Firefox | Safari | Opera |
| Yes | Yes | Yes | Yes | Yes |
Syntax
Number.isNaN(value)
Parameter value
| Parameters | Description |
|---|---|
| value | The value to be tested. |
Return Value
| Type | Description |
|---|---|
| Boolean | If the value is NaN and its type is Number, return true; otherwise return false. |
Technical Details
| JavaScript version: | ECMAScript 6 |
|---|
More examples
Example
Check whether the parameter is an integer:
//
Returns true
isNaN
(
'
Hello
'
)
;
//
Returns false
Number
.
isNaN
(
'
Hello
'
)
;
Try it »
JavaScript Number object