NaN 等于 NaN 吗?
parseFloat("NaN")
返回“NaN”,但
parseFloat("NaN") == "NaN"
返回 false。现在,它确实返回 false 可能是一件好事,但我不明白这是怎么回事。 JavaScript 的创建者只是将其作为一个特例吗?因为否则我无法理解它如何返回 false。
parseFloat("NaN")
returns "NaN", but
parseFloat("NaN") == "NaN"
returns false. Now, that's probably a good thing that it does return false, but I don't understand how this is so. Did the JavaScript creators just make this a special case? Because otherwise I can't understand how this returns false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
更新 2
ECMAScript 6 的新增功能是 Object.is() 函数。这是为了进一步增强
===
检查。这个新函数的好处之一是Object.is(NaN, NaN)
现在将返回 true。如果您能够使用 ECMAScript 6,那么这对您来说将是最具可读性和一致性的解决方案。原始
检查这一点的正确方法是:
如果您要检查的内容是 NaN,则该函数将返回 true。此方法内置于 JavaScript 规范中。
使用 jQuery
jQuery 内置了他们自己的
isNaN
函数,最初是为了帮助解决浏览器之间的一些差异,并添加一些额外的检查,以便可以使用他们的版本而不是 VanillaJS 中的版本。jQuery 更新
在 jQuery 1.7 之后,他们将此函数更改为
$。 isNumeric()
。开关文档
如果您查看这个 Stack Overflow 问题,您会发现很多时候
isNaN()
返回直观的结果是被认为是“不正确”的答案,但根据规范是正确的。避免使用普通
isNaN()
的重要原因之一是null
将返回false
,让您认为它是一个数字。然而,jQuery 函数涵盖了更大范围的直观结果。从他们的文档中:
Update 2
New to ECMAScript 6 is the Object.is() function. This is designed to be a further enhancement of the
===
check. One of the benefits of this new function is thatObject.is(NaN, NaN)
will now return true. If you're able to utilize ECMAScript 6, then this would be the most readable and consistent solution for you.Original
The proper way to check this would be:
If whatever you're checking is a NaN, that function will return true. This method is built into the JavaScript spec.
Using jQuery
jQuery built in their own
isNaN
function originally to help counter some discrepancies between browsers, and add some additional checks so their version can be used instead of the one in VanillaJS.Update for jQuery
After jQuery 1.7, they changed this function to
$.isNumeric()
.Documentation of the switch
If you take a look at this Stack Overflow question, you'll find plenty of times where
isNaN()
returns what would intuitively be considered an "incorrect" answer, but is correct by the spec.One of the big reasons to avoid the vanilla
isNaN()
is thatnull
will returnfalse
, making you think it is a number. However, the jQuery function covers a much larger range of intuitive results.From their documentation:
当 JavaScript 函数返回 NaN 时,这不是文字字符串,而是全局空间中的对象属性。您无法将其与字符串“NaN”进行比较。
请参阅 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/南
When a JavaScript function returns
NaN
, this is not a literal string but an object property in the global space. You cannot compare it to the string"NaN"
.See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/NaN
这是一个特例,NaN 是 Javascript 中唯一不等于自身的东西。
尽管关于字符串与 NaN 对象的其他答案也是正确的。
It's a special case, NaN is the only thing in Javascript not equal to itself.
Although the other answers about strings vs the NaN object are right too.
NaN
是少数不等于自身的对象示例之一。事实上,这个属性就是用来实现常见的bool IsNaN(number)
方法:NaN
is one of the few examples of an object which is not equal to itself. In fact, this very property is used to implement the commonbool IsNaN(number)
method:isNaN
适用于所有非数字的值,但是如果您想专门检查
NaN
或避免类型强制转换;您可以使用
Number .isNaN 代替
isNaN
works for all values that aren't numbersIf, however you want to check for
NaN
specifically, or avoid type coercion;you can use
Number.isNaN
insteadstring
进行比较时Number
(由 ParseFloat 返回)与转换为Number
NaN
不等于任何其他对象(包括NaN
),您会得到
NaN==NaN
。根据第二条规则,这是错误的。Number
(returned by ParseFloat) compares withstring
string
converted toNumber
NaN
is not equal to any other object ( includingNaN
)You get
NaN==NaN
. It is false by second rule.在 ECMAScript 6 中,Object.is() 是 === 的增强。
该方法接受两个参数,如果值相等则返回 true。并且当两个值具有相同类型且具有相同值时被认为是相等的。
这就是原因,因为 console.log(Object.is(NaN, NaN))-->真的
In ECMAScript 6 Object.is() is an enhancement of ===.
This method accepts two arguments and returns true if the values are equivalent.And the two values are considered equivalent when they are of the same type and have the same value.
That's the reason because console.log(Object.is(NaN, NaN))--> TRUE
我正在使用 Google Apps 脚本,所以我坚持使用 ECMA 5。与 Electric Coffee 的答案类似,这是我能够弄清楚的,这似乎给出了一个值是否实际上是的确定答案>NaN,不是如果一个值是
NaN
,而是如果它实际上是NaN
本身:哈哈,这对我来说很有趣
Object.prototype.toString.call(NaN)
等于'[对象编号]'
。我的新手大脑告诉我NaN
是“不是数字”,但遗憾的是它并不是那么简单。编辑:
我想我应该说一下我是如何结束这篇文章的。我的想法是,肯定不包含数字的字符串不会被视为数字......好吧,我最终发现了这一点:
所以即使
' '
是一个非数字字符串,它显然被诱骗成数字 (0
)。然而......
阅读更多内容后,我确实更好地理解了它,但是哇,对于新手来说这是一场精神风暴,哈哈
I'm working with Google Apps Script and so I'm stuck with ECMA 5. Similar to Electric Coffee's answer, here's what I was able to figure out that seems to give a sure answer as to whether or not a value is actually
NaN
, not if a value isNaN
but if it is actuallyNaN
itself:lol Just funny to me that
Object.prototype.toString.call(NaN)
equals'[object Number]'
. My newbie brain tells me thatNaN
is "Not a Number" but sadly it's just not that simple.EDIT:
I guess I should have said how I ended up at this article. I went with the idea that surely a string that doesn't contain a number wouldn't be treated as a number... well, I ended up finding this out:
so even though
' '
is a non-numerical string it apparently gets coaxed into a number (0
).however...
After reading more I do understand it a bit better but wow what a mental crapstorm for a newbie lol