Empty arrays seem to equal true and false at the same time

前端 未结 9 2262
别跟我提以往
别跟我提以往 2020-11-22 10:23

Empty arrays are true but they\'re also equal to false.

9条回答
  •  春和景丽
    2020-11-22 11:04

    Regarding the line:

    if (arr == false) console.log("It's false!");
    

    Maybe these will help:

    console.log(0 == false) // true
    console.log([] == 0) // true
    console.log([] == "") // true
    

    What I believe is happening is that the boolean false is coerced to 0 for comparison with an object (the left-hand side). The object is coerced to a string (the empty string). Then, the empty string is coerced into a number, as well, namely zero. And so the final comparison is 0 == 0, which is true.

    Edit: See this section of the spec for details on exactly how this works.

    Here's what's happening, starting at rule #1:

    1. If Type(x) is different from Type(y), go to step 14.

    The next rule that applies is #19:

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

    The result of ToNumber(false) is 0, so we now have:

    [] == 0
    

    Again, rule #1 tells us to jump to step #14, but the next step that actually applies is #21:

    21. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x)== y.

    The result of ToPrimitive([]) is the empty string, so we now have:

    "" == 0
    

    Again, rule #1 tells us to jump to step #14, but the next step that actually applies is #17:

    17. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x)== y.

    The result of ToNumber("") is 0, which leaves us with:

    0 == 0
    

    Now, both values have the same type, so the steps continue from #1 until #7, which says:

    7. If x is the same number value as y, return true.

    So, we return true.

    In brief:

    ToNumber(ToPrimitive([])) == ToNumber(false)
    

提交回复
热议问题