你如何在 JavaScript 中测试 NaN?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/30314447/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you test for NaN in JavaScript?
提问by Malvolio
I have a variable x and I want to test if x is set to NaN. How do I do that?
我有一个变量 x,我想测试 x 是否设置为 NaN。我怎么做?
My first instinct is probably to, you know, test it, like this:
我的第一直觉可能是,你知道,测试它,像这样:
if (x === NaN) {  ...
Silly rabbit, no, that would be far too easy. NaN is like NULL in SQL, it is not equal to anything, even itself.
傻兔子,不,那太容易了。NaN 就像 SQL 中的 NULL,它不等于任何东西,甚至它本身。
But look, there is a function called isNaN()-- maybe that will do it!
但是看,有一个函数被调用isNaN()——也许可以做到!
No, so far as I can tell, isNaN()is utterly worthless.
不,据我所知,isNaN()完全没有价值。
For example, isNaN([""])properly returns false, but isNaN(["."])returns true.  You don't want do know how I learned about this flaw.
例如,isNaN([""])正确返回 false,但isNaN(["."])返回 true。你不想知道我是如何得知这个缺陷的。
How do I do this?
我该怎么做呢?
Turns out, my question is a duplicate of this one, but the selected answer is wrong. The right answerhas 20% as many upvotes.
采纳答案by Malvolio
The question has the answer if you read closely enough. That is the way I found it: I was typing out the question and... bingo.
如果你仔细阅读,这个问题就会有答案。这就是我找到它的方式:我正在输入问题并......宾果游戏。
You remember when I wrote "NaNis like NULL in SQL, it is not equal to anything, even itself"?  So far as I know, NaNis the only value in Javascript with this property.  Therefore you can write:
你还记得我写的“NaN就像 SQL 中的 NULL,它不等于任何东西,甚至它本身”?据我所知,NaN是 Javascript 中唯一具有此属性的值。因此你可以写:
var reallyIsNaN = function(x) {
   return x !== x;
};
回答by dopeddude
Short Answer
简答
For ECMAScript-5 Users:
对于 ECMAScript-5 用户:
#1
if(x !== x) {
    console.info('x is NaN.');
}
else {
    console.info('x is NOT a NaN.');
}
For people using ECMAScript-6:
对于使用 ECMAScript-6 的人:
#2
Number.isNaN(x);
And For consistency purpose across ECMAScript 5 & 6 both, you can also use this polyfill for Number.isNan
并且为了在 ECMAScript 5 和 6 中保持一致性,您还可以将此polyfill 用于 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;
}
Note: I prefer to test using #1 which works same all places and does not have dependency on latest JS also. (It always gives me correct result. No surprises!)
注意:我更喜欢使用 #1 进行测试,它在所有地方都相同,并且也不依赖于最新的 JS。(它总是给我正确的结果。没有惊喜!)
Detailed Explanation:
详细说明:
Here is our awesome NaN
这是我们很棒的 NaN
NaN == NaN; // false
NaN === NaN; // false
Please don't blame JavaScriptfor this, it is NaN which is supposed to behave this way in other languages also Which is fine as per rationale for all comparisons returning false NaN values
请不要JavaScript为此责怪,它应该是 NaN 在其他语言中也应该以这种方式行事 这根据所有比较返回错误 NaN 值的基本原理很好
So comes isNaNas our savior, but wait it acts little differently in some scenarios like
所以isNaN作为我们的救星来了,但是等等,它在某些情况下的行为几乎没有什么不同,比如
isNaN(undefined); // true
isNaN({});        // true
isNaN("lorem ipsum"); // true
I had some strange faces by seeing the results above. And here comes reason from MDN
看到上面的结果,我有一些奇怪的面孔。这是来自 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.
当 isNaN 函数的参数不是 Number 类型时,该值首先被强制转换为 Number。然后测试结果值以确定它是否为 NaN。
So how should we test NaN for the non-numbers variables at all? I always go by the following
那么我们应该如何测试非数字变量的 NaN 呢?我总是按照以下方式进行
if(x !== x) {
    console.info('Is a NaN');
}
else {
    console.info('Not a NaN');
}
ECMAScript-6/JavaScript-2015 Updates
ECMAScript-6/JavaScript-2015 更新
Do we have anything in ECMAScript-6 for the same. Yup we do...
我们在 ECMAScript-6 中有什么相同的东西吗?是的,我们确实...
Number.isNaN(x); // true
The ES6 implementation will also be helpful for the above cases like
ES6 实现也将有助于上述情况,如
Number.isNaN(undefined); // false
Number.isNaN({}); // false    
Number.isNaN("lorem ipsum"); // false
whereas ECMAScript-5 global function isNaNoutputs in truefor the above cases, which sometimes may not align with our expectation.
而 ECMAScript-5 全局函数isNaN输出在true上述情况下,有时可能与我们的预期不一致。

