为什么这两个不同?为什么IsNaN(x)与x = NaN不同x = NaN
var x = NaN; //e.g. Number("e");
alert(isNaN(x)); //true (good)
alert(x == NaN); //false (bad)
为什么这两个不同?为什么IsNaN(x)与x = NaN不同x = NaN
var x = NaN; //e.g. Number("e");
alert(isNaN(x)); //true (good)
alert(x == NaN); //false (bad)
没有等于NaN
。任何比较将始终为false
。
在严格和抽象比较算法中,如果类型相同,且操作数为NaN
,则结果为false
。
如果Type(x)是号码,然后
- 如果
x
是NaN
,返回false
。- 如果
y
是NaN
,则返回false
。
在抽象的算法,如果类型是不同的,并且NaN
是操作数,那么另一个操作数最终会被裹挟到了一些之一,并把我们带回上述情形。
真的吗? Infinity看起来像NaN一样。 Infinity == Infinity怎么样?它如何回归真实? – allenhwkim 2013-02-20 18:38:14
@ bighostkim:*“Infinity看起来像NaN一样......”*我不明白你的意思。他们不一样。 – 2013-02-20 18:40:30
typeof Infinity是数字,与type of NaN相同。正如你所说,另一个操作数最终将被强制为一个数字。 – allenhwkim 2013-02-20 18:47:26
下面的操作都将返回NaN
The divisions 0/0, ∞/∞, ∞/−∞, −∞/∞, and −∞/−∞
The multiplications 0×∞ and 0×−∞
The power 1^∞
The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions.
Real operations with complex results:
The square root of a negative number
The logarithm of a negative number
The tangent of an odd multiple of 90 degrees (or π/2 radians)
The inverse sine or cosine of a number which is less than −1 or greater than +1.
以下操作的数字操作返回值。因此,typeof
南是一个数字。 NaN是数学术语中未定义的数字。 ∞+(-∞)不等于∞+(-∞)。但是我们得到NaN是typeof
,因为它是由数字操作产生的。
从wiki:
为什么downvote,家伙?至少告诉我,以便我可以改善。 :| – ppsreejith 2013-02-20 17:49:00
我没有投票,但对我来说很明显,你还没有回答问题**为什么**。 – ChaosPandion 2013-02-20 17:57:39
@ChaosPandion,不是很明显吗?以下操作返回数值操作的值,因此typeof Nan是一个数字。 NaN是数学术语中未定义的数字。 ∞+(-∞)不等于∞+(-∞)。但是我们知道NaN是typeof数字,因为它是由数字操作产生的。这就是我写的。 – ppsreejith 2013-02-20 18:02:34
考虑'NaN === NaN // false'。鸭子不是兔子'鸭子!==兔子',但都不是_Number_,所以都是'NaN',因此'NaN'不能等于它自己,因此'=='和'==='必须返回'false'。 – 2013-02-20 17:39:36
http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/ – Almo 2013-02-20 17:41:03
想一想为什么他们首先需要一个'isNaN'函数。 – ChaosPandion 2013-02-20 17:41:27