比较 Javascript 中的 NaN 值是否相等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8965364/
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
Comparing NaN values for equality in Javascript
提问by GOTO 0
I need to compare two numeric values for equality in Javascript. The values may be NaN
as well.
I've come up with this code:
我需要在 Javascript 中比较两个数值是否相等。值也可能是NaN
。我想出了这个代码:
if (val1 == val2 || isNaN(val1) && isNaN(val2)) ...
which is working fine, but it looks bloated to me. I would like to make it more concise. Any ideas?
这工作正常,但对我来说看起来很臃肿。我想让它更简洁。有任何想法吗?
采纳答案by Anant
Try using Object.is()
, it determines whether two values are the same value. Two values are the same if one of the following holds:
尝试使用Object.is()
,它确定两个值是否相同。如果以下条件之一成立,则两个值相同:
- both
undefined
- both
null
- both
true
or bothfalse
- both strings of the same length with the same characters in the same order
- both the same object
- both numbers and
- both
+0
- both
-0
- both
NaN
- or both non-zero and both not
NaN
and both have the same value
- both
- 两个都
undefined
- 两个都
null
- 两者
true
或两者false
- 两个长度相同的字符串以相同的顺序具有相同的字符
- 都是同一个对象
- 数字和
- 两个都
+0
- 两个都
-0
- 两个都
NaN
- 或两者都非零且两者都不是
NaN
且两者都具有相同的值
- 两个都
e.g. Object.is(NaN, NaN)
=> true
例如Object.is(NaN, NaN)
=>true
Refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
参考https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
回答by ThiefMaster
if(val1 == val2 || (isNaN(val1) && isNaN(val2)))
Nothing to improve. Just add the parentheses to make it clear to everyone.
没什么好改进的。只需添加括号,让每个人都清楚。
回答by davidchambers
Avoid isNaN
. Its behaviour is misleading:
避免isNaN
。它的行为具有误导性:
isNaN(undefined) // true
_.isNaN
(from Underscore.js) is an elegant function which behaves as expected:
_.isNaN
(来自Underscore.js)是一个优雅的函数,它的行为符合预期:
// Is the given value `NaN`?
//
// `NaN` is the only value for which `===` is not reflexive.
_.isNaN = function(obj) {
return obj !== obj;
};
_.isNaN(undefined) // false
_.isNaN(0/0) // true
回答by Esailija
if ( val1 === val2 )
if ( val1 === val2 )
If either one or both are NaN
it will evaluate to false.
如果其中一个或两个都是NaN
,它将评估为 false。
Also, NaN !== NaN
还, NaN !== NaN
回答by Joachim Isaksson
NaN is never equal to itself no matter the comparison method, so the only more concise solution for your problem that I can think of would be to create a function call with a descriptive name for doing this rather special comparison and use that comparison function in your code instead.
无论采用哪种比较方法,NaN 都永远不会等于自身,因此我能想到的唯一更简洁的解决方案是创建一个具有描述性名称的函数调用,以进行此相当特殊的比较,并在您的项目中使用该比较函数代码代替。
That would also have the advantage of localizing changes to the algorithm the day you decide that undefined should be equal to undefined too.
这也具有在您决定 undefined 也应等于 undefined 的那一天对算法进行本地化更改的优势。
回答by Grace Shao
As long as you know these two variables are numeric, you can try:
只要你知道这两个变量是数字,你就可以试试:
if (val1 + '' == val2 + '')
It turns the two values into strings. A funny answer, but it should work. :)
它将两个值转换为字符串。一个有趣的答案,但它应该有效。:)
回答by mario ruiz
And what's about the function Number.isNaN()? I believe this must be used whenever is possible.
函数Number.isNaN() 是什么?我相信只要有可能就必须使用它。
> NaN === NaN
false
> Number.isNaN
? isNaN() { [native code] }
> Number.isNaN() === Number.isNaN()
true
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
回答by dopeddude
For Numeric cases the solution is fine but to extend it to work for other data-types as well my suggestion would be as follows:
对于数字情况,解决方案很好,但要将其扩展为适用于其他数据类型,我的建议如下:
if(val1 === val2 || (val1 !== val1 && val2 !== val2))
Reason being global isNaN
is erroneous. It will give you wrong results in scenarios like
理由是全球性的isNaN
是错误的。它会在以下情况下给您错误的结果
isNaN(undefined); // true
isNaN({}); // true
isNaN("lorem ipsum"); // true
I have posted a comprehensive answer here which covers the NaN comparison for equality as well.
我在这里发布了一个全面的答案,其中也涵盖了 NaN 的相等性比较。
回答by GOTO 0
I created this answer after reviewing the suggestions of ThiefMaster, Esailija, Joachim Isaksson and davidchambers. Can this be further improved?
在查看了 ThiefMaster、Esailija、Joachim Isaksson 和 davidchambers 的建议后,我创建了这个答案。这可以进一步改进吗?
// Determines if two numeric values are equal.
// Also returns true when both parameters are NaN.
function areEqualNumeric(val1, val2) {
return val1 === val2 || (val1 !== val1 && val2 !== val2);
}
回答by Pauline Orr
Why not an if statement like this?
为什么不是这样的 if 语句?
if (isNaN(x) == true){
alert("This is not a number.");
}