isNaN()
function determines whether a value is
NaN
or not. Note, coercion inside the
isNaN
function has
interesting
rules; you may alternatively want to use
Number.isNaN()
, as defined in ECMAScript 2015.
isNaN(value)
value
The value to be tested.
true
if the given value is
NaN
; otherwise,
false
.
isNaN
function
Unlike all other possible values in JavaScript, it is not possible to rely on the equality operators (== and ===) to determine whether a value
is
NaN
or not, because both
NaN == NaN
and
NaN === NaN
evaluate to
false
. Hence, the necessity of an
isNaN
函数。
NaN
值
NaN
values are generated when arithmetic operations result in
undefined
or
unrepresentable
values. Such values do not necessarily represent overflow conditions. A
NaN
also results from attempted coercion to numeric values of non-numeric values for which no primitive numeric value is available.
For example, dividing zero by zero results in a
NaN
— but dividing other numbers by zero does not.
Since the very earliest versions of the
isNaN
function specification, its behavior for non-numeric arguments has been confusing. When the argument to the
isNaN
function is not of type
Number
, the value is first coerced to a Number. The resulting value is then tested to determine whether it is
NaN
. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number." The confusion stems from the fact that the term, "not a number", has a specific meaning for numbers represented as IEEE-754 floating-point values. The function should be interpreted as answering the question, "is this value, when coerced to a numeric value, an IEEE-754 'Not A Number' value?"
ECMAScript 2015 contains the
Number.isNaN()
函数。
Number.isNaN(x)
is a reliable way to test whether
x
is
NaN
or not. Even with
Number.isNaN
, however, the meaning of
NaN
remains the precise numeric meaning and not simply, "not a number". Alternatively, in the absence of
Number.isNaN
, the expression
(x != x)
is a more reliable way to test whether variable
x
is
NaN
or not, as the result is not subject to the false positives that make
isNaN
unreliable.
A polyfill for
isNaN
would be (the polyfill leverages the unique never-equal-to-itself characteristic of
NaN
):
var isNaN = function(value) {
var n = Number(value);
return n !== n;
};
isNaN(NaN); // true
isNaN(undefined); // true
isNaN({}); // true
isNaN(true); // false
isNaN(null); // false
isNaN(37); // false
// strings
isNaN('37'); // false: "37" is converted to the number 37 which is not NaN
isNaN('37.37'); // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN("37,5"); // true
isNaN('123ABC'); // true: parseInt("123ABC") is 123 but Number("123ABC") is NaN
isNaN(''); // false: the empty string is converted to 0 which is not NaN
isNaN(' '); // false: a string with spaces is converted to 0 which is not NaN
// dates
isNaN(new Date()); // false
isNaN(new Date().toString()); // true
// This is a false positive and the reason why isNaN is not entirely reliable
isNaN('blabla'); // true: "blabla" is converted to a number.
// Parsing this as a number fails and returns NaN
There is a more usage oriented way to think of
isNaN()
: If
isNaN(x)
返回
false
, you can use
x
in an arithmetic expression not making the expression return
NaN
。若它返回
true
,
x
will make every arithmetic expression return
NaN
. This means that in JavaScript,
isNaN(x) == true
相当于
x - 0
returning
NaN
(though in JavaScript
x - 0 == NaN
always returns false, so you can't test for it). Actually,
isNaN(x)
,
isNaN(x - 0)
,
isNaN(Number(x))
,
Number.isNaN(x - 0)
,和
Number.isNaN(Number(x))
always return the same and in JavaScript
isNaN(x)
is just the shortest possible form to express each of these terms.
You can use this, for example, to test whether an argument to a function is arithmetically processable (usable "like" a number), or if it's not and you have to provide a default value or something else. This way you can have a function that makes use of the full versatility JavaScript provides by implicitly converting values depending on context.
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'isNaN' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
isNaN
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持