为什么 NaN = = = NaN 假?

为什么 NaN === NaN在 Javascript 中返回 false

> undefined === undefined
true
> NaN === NaN
false
> a = NaN
NaN
> a === a
false

在美国广播公司的节目中,我看到了这样的画面:

对 NaN 的测试

相等运算符(=====)不能用于针对 NaN测试值。请改为使用 isNaN

这个问题有什么参考答案吗? 欢迎提供。

40190 次浏览

Strict answer: Because the JS spec says so:

  • If Type(x) is Number, then
    • If x is NaN, return false.
    • If y is NaN, return false.

Useful answer: The IEEE 754 spec for floating-point numbers (which is used by all languages for floating-point) says that NaNs are never equal.

This behaviour is specified by the IEEE-754 standard (which the JavaScript spec follows in this respect).

For an extended discussion, see What is the rationale for all comparisons returning false for IEEE754 NaN values?

Although either side of NaN===NaN contains the same value and their type is Number but they are not same. According to ECMA-262, either side of == or === contains NaN then it will result false value.

you may find a details rules in here-

http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3