NaN 等于 NaN 吗?

发布于 2024-11-28 12:22:12 字数 215 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浪荡不羁 2024-12-05 12:22:12

更新 2

ECMAScript 6 的新增功能是 Object.is() 函数。这是为了进一步增强 === 检查。这个新函数的好处之一是 Object.is(NaN, NaN) 现在将返回 true。如果您能够使用 ECMAScript 6,那么这对您来说将是最具可读性和一致性的解决方案。

原始

检查这一点的正确方法是:

isNaN(parseInt(variable))

如果您要检查的内容是 NaN,则该函数将返回 true。此方法内置于 JavaScript 规范中。

使用 jQuery

jQuery 内置了他们自己的 isNaN 函数,最初是为了帮助解决浏览器之间的一些差异,并添加一些额外的检查,以便可以使用他们的版本而不是 VanillaJS 中的版本。

jQuery 更新

在 jQuery 1.7 之后,他们将此函数更改为 $。 isNumeric()

开关文档

如果您查看这个 Stack Overflow 问题,您会发现很多时候 isNaN() 返回直观的结果是被认为是“不正确”的答案,但根据规范是正确的。

避免使用普通 isNaN() 的重要原因之一是 null 将返回 false,让您认为它是一个数字。然而,jQuery 函数涵盖了更大范围的直观结果。

从他们的文档中:

从 jQuery 3.0 开始,$.isNumeric() 仅当参数为
数字类型,或者如果它是字符串类型并且可以强制转换为
有限的数字。在所有其他情况下,它返回 false。

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 that Object.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:

isNaN(parseInt(variable))

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 that null will return false, making you think it is a number. However, the jQuery function covers a much larger range of intuitive results.

From their documentation:

As of jQuery 3.0 $.isNumeric() returns true only if the argument is of
type number, or if it's of type string and it can be coerced into
finite numbers. In all other cases, it returns false.

揽清风入怀 2024-12-05 12:22:12

当 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

恬淡成诗 2024-12-05 12:22:12

这是一个特例,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.

清旖 2024-12-05 12:22:12

NaN 是少数不等于自身的对象示例之一。事实上,这个属性就是用来实现常见的 bool IsNaN(number) 方法:

function isNaN(x)
{ 
    return x != x; 
}

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 common bool IsNaN(number) method:

function isNaN(x)
{ 
    return x != x; 
}
雄赳赳气昂昂 2024-12-05 12:22:12

isNaN 适用于所有非数字的值

isNaN('foo') == true // a string is indeed not a number
isNaN(NaN) == true
isNaN(12) == false
isNaN([1,2,3]) == true // an array is also not a number

,但是如果您想专门检查 NaN 或避免类型强制转换;
您可以使用 Number .isNaN 代替

Number.isNaN('foo') == false
Number.isNaN(NaN) == true
Number.isNaN(12) == false
Number.isNaN([1,2,3]) == false

isNaN works for all values that aren't numbers

isNaN('foo') == true // a string is indeed not a number
isNaN(NaN) == true
isNaN(12) == false
isNaN([1,2,3]) == true // an array is also not a number

If, however you want to check for NaN specifically, or avoid type coercion;
you can use Number.isNaN instead

Number.isNaN('foo') == false
Number.isNaN(NaN) == true
Number.isNaN(12) == false
Number.isNaN([1,2,3]) == false
怪我鬧 2024-12-05 12:22:12
  • string 进行比较时
  • Number (由 ParseFloat 返回)与转换为 Number NaN 不等于任何其他对象(包括 NaN),

您会得到 NaN==NaN 。根据第二条规则,这是错误的。

  • When Number (returned by ParseFloat) compares with string string converted to Number
  • NaN is not equal to any other object ( including NaN)

You get NaN==NaN . It is false by second rule.

怪我太投入 2024-12-05 12:22:12

在 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

遥远的她 2024-12-05 12:22:12

我正在使用 Google Apps 脚本,所以我坚持使用 ECMA 5。与 Electric Coffee 的答案类似,这是我能够弄清楚的,这似乎给出了一个值是否实际上是的确定答案>NaN,不是如果一个值是 NaN,而是如果它实际上是 NaN 本身:

function isThisNaN(x)
{ 
    return isNaN(x) && Object.prototype.toString.call(x) === '[object Number]'; 
}
console.log(isThisNaN(NaN)); // true

哈哈,这对我来说很有趣Object.prototype.toString.call(NaN) 等于'[对象编号]'。我的新手大脑告诉我 NaN 是“不是数字”,但遗憾的是它并不是那么简单。

编辑:
我想我应该说一下我是如何结束这篇文章的。我的想法是,肯定不包含数字的字符串不会被视为数字......好吧,我最终发现了这一点:

isNaN('a'); // true
isNaN(' '); // false

所以即使 ' ' 是一个非数字字符串,它显然被诱骗成数字 (0)。

console.log(Number(' ')); // 0.0

然而......

console.log( 0  ? true : false); // false
console.log(' ' ? true : false); // true

阅读更多内容后,我确实更好地理解了它,但是哇,对于新手来说这是一场精神风暴,哈哈

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 is NaN but if it is actually NaN itself:

function isThisNaN(x)
{ 
    return isNaN(x) && Object.prototype.toString.call(x) === '[object Number]'; 
}
console.log(isThisNaN(NaN)); // true

lol Just funny to me that Object.prototype.toString.call(NaN) equals '[object Number]'. My newbie brain tells me that NaN 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:

isNaN('a'); // true
isNaN(' '); // false

so even though ' ' is a non-numerical string it apparently gets coaxed into a number (0).

console.log(Number(' ')); // 0.0

however...

console.log( 0  ? true : false); // false
console.log(' ' ? true : false); // true

After reading more I do understand it a bit better but wow what a mental crapstorm for a newbie lol

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文