4

I've been playing around with arrays in JavaScript and cannot figure out why this happens:

console.log(0 == 0)
//true

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

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

console.log([] == [])
//false

console.log([] == ![])
// true

The empty array is equal enough to zero both left and right, but why isn't it equal to itself?

I realise that comparing two objects would not result true, but why are they coerced to 0 (or falsy, which shouldn't be the case) if you compare them to 0, while threated as an object if you compare them to the other array?

Randy
  • 9,419
  • 5
  • 39
  • 56
  • 1
    http://stackoverflow.com/questions/30820611/javascript-arrays-cannot-equal-each-other – sinisake Dec 28 '16 at 11:14
  • 1
    Possible duplicate of [Why are two identical objects not equal to each other?](http://stackoverflow.com/questions/11704971/why-are-two-identical-objects-not-equal-to-each-other) – baao Dec 28 '16 at 11:16
  • @sinisake That would explain why they don't match each other, but the fact they match `0` is confusing me completely – Randy Dec 28 '16 at 11:16
  • 1
    but `console.log([] === 0)` gives `false` – RomanPerekhrest Dec 28 '16 at 11:17
  • @Randy, i didn't dig so deep in javascript under the hood, but i guess type coercion is in question (if that is right term): https://jsfiddle.net/pfy0e9qv/ http://stackoverflow.com/questions/19915688/what-exactly-is-type-coercion-in-javascript – sinisake Dec 28 '16 at 11:21
  • 1
    JS is a very loosely typed language and polymorphism runs through the veins of it. So i would advice you to study [type coercion in JS](https://code.tutsplus.com/articles/the-beginners-guide-to-type-coercion-a-practical-example--cms-21998) topic. – Redu Dec 28 '16 at 11:29
  • [] != [], because of them being different objects, other values are same because of == operator doing type conversion before comparison, You can view useful explanations from these threads, http://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons http://stackoverflow.com/questions/5491605/empty-arrays-seem-to-equal-true-and-false-at-the-same-time – Oscar Dec 28 '16 at 11:40

4 Answers4

8
console.log(0 == [])
//true 

You are trying to compare object with an integer, so your object is implicitly typecasted to equivalent integer value that is 0

console.log([] == [])
//false 

as two objects are never equal

Randy
  • 9,419
  • 5
  • 39
  • 56
Achal Saini
  • 155
  • 6
2
console.log([] == [])

That will compare whether array1 and array2 are the same array object in memory, which is not what you want.

In order to do what you want, you'll need to check whether the two arrays have the same length, and that each member in each index is identical.

console.log([].length == [].length)
// true
Sandeep Kapil
  • 984
  • 8
  • 14
1

Since the complete answer is never given and I actually understand it now, I'll provide the answer myself.

I found this in the Ecma-262 pdf:

It basically reads that [] == 0 is the same as Number([]) == 0 which is the same as 0 == 0 which is true. This does not apply to strict ===.

There is no rule to compare objects other then rule number one, which is x is the same as y. This means the same in everything, also memory address. Since they are not sharing the same memory address, rule 10 applies (return false).

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

    a. Return the result of performing Strict Equality Comparison x === y.

  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, Number, or Symbol 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, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
  10. Return false
Randy
  • 9,419
  • 5
  • 39
  • 56
0

This question is handled with the knowledge of object reference and type conversion more properly.First,in javascript, object value is stored by reference.So we can tell it is different from [] and [],because the two array correspond with two different addr in memory.Second, '==' is a not rigorous operation for both left and right, [] and 0 are both transformed to false.

Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
王吉志
  • 77
  • 3