javascript – 为什么是undefined == undefined但NaN!= NaN?

前端之家收集整理的这篇文章主要介绍了javascript – 为什么是undefined == undefined但NaN!= NaN?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_1@参见英文答案 > What is the rationale for all comparisons returning false for IEEE754 NaN values?9个
我想知道为什么undefined == undefined但NaN!= NaN.

解决方法

因为这是 Abstract Equality Comparison AlgorithmStrict Equality Comparison Algorithm中的定义.

如果任一操作数为==或===为NaN,则返回false.

抽象

  • If Type(x) is Number,then
    • If x is NaN,return false.
    • If y is NaN,return false.
    • If x is the same Number value as y,return true.
    • If x is +0 and y is −0,return true.
    • If x is −0 and y is +0,return true.
    • Return false.

编辑:@CMS所述的不平等比较动机符合IEEE 754标准.

从以下评论中提供的维基百科链接

…The normal comparison operations however treat NaNs as unordered and compare −0 and +0 as equal. The totalOrder predicate will order these cases,and it also distinguishes between different representations of NaNs and between the same decimal floating point number encoded in different ways.

猜你在找的JavaScript相关文章