1
        var array1=[1,2,3];
        var array2=[1,2,3];
        alert((array1<array2)+(array1==array2)+(array1>array2));

As alert returns 0, array1 is not greater, not less and not equal to array2.
The question is:
How does array1 relates to array2?

Edit: my question is: How does array1 relates to array2?

AlieN
  • 555
  • 2
  • 16
  • See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators and the note about object references and equality. – Michael Berkowski Apr 12 '14 at 12:56
  • See [this](http://stackoverflow.com/questions/23040656/duplicate-distinct-and-unique-values-in-array-javascript/23040704#23040704) post, probably find something interesting – nanobash Apr 13 '14 at 16:14

2 Answers2

4

The two array array1 and array2 are never equal as they are different instances.

If you want to compare them, you can do:

array1.join() == array2.join() // true

And BTW, the alert() doesn't alert false it alerts 0

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

Weird thing about arrays, they are never equal since they're not the same object.

Although, arrays act strange when you compare them with < or >

We can use this weird behavior to our advantage to check whether two arrays have the same content:

function arraysEqual(a, b) { return !(a < b || b < a); }

Or

function arraysEqual(a, b) { return !(a < b) && !(b < a); }

Which means, if the no array is bigger than the other, they're equal.


Source: How to check if two arrays are equal with JavaScript?

Community
  • 1
  • 1
assembly_wizard
  • 2,034
  • 1
  • 17
  • 10