Number.isNaN()
method determines whether the passed value is
NaN
and its type is
Number
. It is a more robust version of the original, global
isNaN()
.
Number.isNaN(value)
value
NaN
.
true
if the given value is
NaN
and its type is
Number
; otherwise,
false
.
Due to both equality operators,
==
and
===
, evaluating to
false
when checking if
NaN
is
NaN
, the function
Number.isNaN()
has become necessary. This situation is unlike all other possible value comparisons in JavaScript.
In comparison to the global
isNaN()
function,
Number.isNaN()
doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to
NaN
, but aren't actually the same value as
NaN
. This also means that only values of the type number, that are also
NaN
, return
true
.
The following works because NaN is the only value in javascript which is not equal to itself.
Number.isNaN = Number.isNaN || function isNaN(input) {
return typeof input === 'number' && input !== input;
}
Number.isNaN(NaN); // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0); // true
// e.g. these would have been true with global isNaN()
Number.isNaN('NaN'); // false
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN('blabla'); // false
// These all return false
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);
Number.isNaN('37');
Number.isNaN('37.37');
Number.isNaN('');
Number.isNaN(' ');
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Number.isnan' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
isNaN
|
Chrome 25 | Edge 12 | Firefox 15 | IE No | Opera 15 | Safari 9 | WebView Android ≤37 | Chrome Android 25 | Firefox Android 15 | Opera Android 14 | Safari iOS 9 | Samsung Internet Android 1.5 | nodejs 0.10 |
完整支持
不支持
Number
Number.isFinite()
Number.isInteger()
Number.isNaN()
Number.isSafeInteger()
Number.parseFloat()
Number.parseInt()
Number.prototype.toExponential()
Number.prototype.toFixed()
Number.prototype.toLocaleString()
Number.prototype.toPrecision()
Number.prototype.toSource()
Number.prototype.toString()
Number.prototype.valueOf()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()