The Number.isNaN()
method determines whether the passed value is NaN
. It is a more robust version of the original, global isNaN()
.
Number.isNaN(value)
value
NaN
.A Boolean
indicating whether or not the given value is NaN
.
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
.
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(' ');
Number.isNaN = Number.isNaN || function(value) { return typeof value === 'number' && isNaN(value); } // Or Number.isNaN = Number.isNaN || function(value) { return value !== value; }
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Number.isnan' in that specification. | Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Number.isnan' in that specification. | Draft |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 25 | 15 (15) | No support | (Yes) | 9 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | ? | 15.0 (15) | No support | No support | 9 |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN