Why [] == [] is false in JavaScript?

前端 未结 6 1734
时光说笑
时光说笑 2020-12-03 17:32

I am working on a part of the code where I have an array which looks like [[data]]. The data is rendered on the server side through the Django temp

6条回答
  •  春和景丽
    2020-12-03 18:32

    The expression [] == [] has an equivalent notation of:

    new Array() == new Array()
    

    And knowing that Array is also an Object, the behaviour of the comparison is unambiguously explained in The Abstract Equality Comparison Algorithm section of the ECMAScript Language Specification:

    The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

    1. If Type(x) is the same as Type(y), then
      1. If Type(x) is Undefined, return true.
      2. If Type(x) is Null, return true.
      3. If Type(x) is Number, then
        1. If x is NaN, return false.
        2. If y is NaN, return false.
        3. If x is the same Number value as y, return true.
        4. If x is +0 and y is −0, return true.
        5. If x is −0 and y is +0, return true.
        6. Return false.
      4. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
      5. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
      6. Return true if x and y refer to the same object. Otherwise, return false.
    2. If x is null and y is undefined, return true.
    3. If x is undefined and y is null, return true.
    4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
    5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
    6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
    7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
    8. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
    9. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
    10. Return false.

    Your comparison result is explained by 1.6, highlighted above.

    Alternative expression

    In your case I would suggest to simply use this condition instead:

    if (a[0].length == 0) {
        console.log('no data');
    }
    

提交回复
热议问题