Javascript NaN 等于 NaN 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6976721/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:05:15  来源:igfitidea点击:

Is NaN equal to NaN?

javascriptfunctionnanparseintparsefloat

提问by joseph

parseFloat("NaN")

returns "NaN", but

返回“NaN”,但

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.

返回假。现在,它确实返回 false 可能是一件好事,但我不明白这是怎么回事。JavaScript 创建者是否只是将其作为特例?因为否则我无法理解这是如何返回 false 的。

采纳答案by Phil

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".

当 JavaScript 函数返回 时NaN,这不是文字字符串,而是全局空间中的对象属性。您无法将其与 string 进行比较"NaN"

See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/NaN

请参阅https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/NaN

回答by krillgar

Update 2

更新 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.

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

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.

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

Using jQuery

使用 jQuery

jQuery built in their own isNaNfunction 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.

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

Update for jQuery

更新 jQuery

After jQuery 1.7, they changed this function to $.isNumeric().

在 jQuery 1.7 之后,他们将此函数更改为$.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.

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

One of the big reasons to avoid the vanilla isNaN()is that nullwill return false, making you think it is a number. However, the jQuery function covers a much larger range of intuitive results.

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

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.

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

回答by shelman

It's a special case, NaN is the only thing in Javascript not equal to itself.

这是一个特例,NaN 是 Javascript 中唯一不等于自身的东西。

Although the other answers about strings vs the NaN object are right too.

尽管关于字符串与 NaN 对象的其他答案也是正确的。

回答by Martin Devillers

NaNis 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:

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

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

回答by Electric Coffee

isNaNworks for all values that aren't numbers

isNaN适用于所有不是数字的值

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

If, however you want to check for NaNspecifically, or avoid type coercion;
you can use Number.isNaNinstead

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

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

回答by RiaD

  • When Number(returned by ParseFloat) compares with stringstringconverted to Number
  • NaNis not equal to any other object ( including NaN)
  • Number(由 ParseFloat 返回)与stringstring转换为Number
  • NaN不等于任何其他对象(包括NaN

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

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

回答by SatoK

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

在 ECMAScript 6 中 Object.is() 是 === 的增强。此方法接受两个参数,如果值相等则返回 true。当这两个值属于相同类型且具有相同值时,它们被认为是相等的。这是因为 console.log(Object.is(NaN, NaN))--> TRUE

回答by Cecell

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 NaNbut if it is actually NaNitself:

我正在使用 Google Apps Script,所以我坚持使用 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

lol Just funny to me that Object.prototype.toString.call(NaN)equals '[object Number]'. My newbie brain tells me that NaNis "Not a Number" but sadly it's just not that simple.

大声笑对我来说这Object.prototype.toString.call(NaN)等于'[object Number]'。我的新手大脑告诉我这NaN不是一个数字,但遗憾的是它并没有那么简单。

EDIT: I guess I should have said how I ended up at this article. I went with the idea that surelya 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).

所以即使' '是一个非数字字符串,它显然也会被哄成一个数字 ( 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

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