如何在 JavaScript 中测试 NaN?
How do you test for NaN in JavaScript?
我有一个变量 x,我想测试 x 是否设置为 NaN。我该怎么做?
我的第一直觉可能是,你知道的,像这样测试它:
if (x === NaN) { ...
傻兔子,不,那太容易了。 NaN 就像 SQL 中的 NULL,它不等于任何东西,甚至不等于它自己。
但是看,有一个名为 isNaN()
的函数——也许就可以了!
不,据我所知,isNaN()
一文不值。
例如,isNaN([""])
正确 returns 错误,但 isNaN(["."])
returns 正确。你不想知道我是怎么知道这个缺陷的。
我该怎么做?
事实证明,我的问题是 this one, but the selected answer is wrong. The right answer 的重复,有 20% 的赞成票。
如果你仔细阅读,这个问题就有了答案。这就是我找到它的方式:我正在输入问题然后......宾果游戏。
你记得我写过“NaN
就像 SQL 中的 NULL,它不等于任何东西,甚至它本身”?据我所知,NaN
是 Javascript 中唯一具有此 属性 的值。因此你可以这样写:
var reallyIsNaN = function(x) {
return x !== x;
};
简答
对于 ECMAScript-5 用户:
#1
if(x !== x) {
console.info('x is NaN.');
}
else {
console.info('x is NOT a NaN.');
}
对于使用 ECMAScript-6 的人:
#2
Number.isNaN(x);
并且为了在 ECMAScript 5 和 6 中保持一致,您也可以使用这个 polyfill for Number.isNan
#3
//Polyfill from MDN
Number.isNaN = Number.isNaN || function(value) {
return typeof value === "number" && isNaN(value);
}
// Or
Number.isNaN = Number.isNaN || function(value) {
return value !== value;
}
注意:我更喜欢使用 #1 进行测试,它在所有地方都可以正常工作,并且也不依赖于最新的 JS。 (它总是给我正确的结果。没有惊喜!)
详细解释:
这是我们很棒的 NaN
NaN == NaN; // false
NaN === NaN; // false
请不要为此责怪 JavaScript
,NaN 在其他语言中也应该以这种方式表现,根据 rationale for all comparisons returning false NaN values[=63 这很好=]
isNaN
作为我们的救世主来了,但等等,它在某些情况下的表现略有不同,例如
isNaN(undefined); // true
isNaN({}); // true
isNaN("lorem ipsum"); // true
看到上面的结果,我有些奇怪。这是来自 MDN
的原因
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.
那么我们应该如何测试非数字变量的 NaN?我总是遵循以下
if(x !== x) {
console.info('Is a NaN');
}
else {
console.info('Not a NaN');
}
ECMAScript-6/JavaScript-2015 更新
我们在 ECMAScript-6 中是否有相同的内容。是的,我们做...
Number.isNaN(x); // true
ES6 实现也将有助于上述情况,例如
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN("lorem ipsum"); // false
而 ECMAScript-5 全局函数 isNaN
在上述情况下输出 true
,有时可能与我们的预期不符。
我有一个变量 x,我想测试 x 是否设置为 NaN。我该怎么做?
我的第一直觉可能是,你知道的,像这样测试它:
if (x === NaN) { ...
傻兔子,不,那太容易了。 NaN 就像 SQL 中的 NULL,它不等于任何东西,甚至不等于它自己。
但是看,有一个名为 isNaN()
的函数——也许就可以了!
不,据我所知,isNaN()
一文不值。
例如,isNaN([""])
正确 returns 错误,但 isNaN(["."])
returns 正确。你不想知道我是怎么知道这个缺陷的。
我该怎么做?
事实证明,我的问题是 this one, but the selected answer is wrong. The right answer 的重复,有 20% 的赞成票。
如果你仔细阅读,这个问题就有了答案。这就是我找到它的方式:我正在输入问题然后......宾果游戏。
你记得我写过“NaN
就像 SQL 中的 NULL,它不等于任何东西,甚至它本身”?据我所知,NaN
是 Javascript 中唯一具有此 属性 的值。因此你可以这样写:
var reallyIsNaN = function(x) {
return x !== x;
};
简答
对于 ECMAScript-5 用户:
#1
if(x !== x) {
console.info('x is NaN.');
}
else {
console.info('x is NOT a NaN.');
}
对于使用 ECMAScript-6 的人:
#2
Number.isNaN(x);
并且为了在 ECMAScript 5 和 6 中保持一致,您也可以使用这个 polyfill for Number.isNan
#3
//Polyfill from MDN
Number.isNaN = Number.isNaN || function(value) {
return typeof value === "number" && isNaN(value);
}
// Or
Number.isNaN = Number.isNaN || function(value) {
return value !== value;
}
注意:我更喜欢使用 #1 进行测试,它在所有地方都可以正常工作,并且也不依赖于最新的 JS。 (它总是给我正确的结果。没有惊喜!)
详细解释:
这是我们很棒的 NaN
NaN == NaN; // false
NaN === NaN; // false
请不要为此责怪 JavaScript
,NaN 在其他语言中也应该以这种方式表现,根据 rationale for all comparisons returning false NaN values[=63 这很好=]
isNaN
作为我们的救世主来了,但等等,它在某些情况下的表现略有不同,例如
isNaN(undefined); // true
isNaN({}); // true
isNaN("lorem ipsum"); // true
看到上面的结果,我有些奇怪。这是来自 MDN
的原因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.
那么我们应该如何测试非数字变量的 NaN?我总是遵循以下
if(x !== x) {
console.info('Is a NaN');
}
else {
console.info('Not a NaN');
}
ECMAScript-6/JavaScript-2015 更新
我们在 ECMAScript-6 中是否有相同的内容。是的,我们做...
Number.isNaN(x); // true
ES6 实现也将有助于上述情况,例如
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN("lorem ipsum"); // false
而 ECMAScript-5 全局函数 isNaN
在上述情况下输出 true
,有时可能与我们的预期不符。