Why does “undefined equals false” return false?

后端 未结 7 1143
借酒劲吻你
借酒劲吻你 2020-11-30 06:28

When I compare undefined and null against Boolean false, the statement returns false:

undefined == false;
null == false;

It return false. W

7条回答
  •  爱一瞬间的悲伤
    2020-11-30 06:51

    According to the specification which was mentioned above:

    If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

    Number(undefined) = NaN;
    
    false == NaN // false
    

    Moreover if we change order false == undefined

    If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

    Number(false) = 0;
    0 == undefined
    

    There is no rule for this case, so work the default behavior:

    Return false.

提交回复
热议问题