Why doesn't equality check work with arrays [duplicate]

I started with:

"1:2".split(':') == ["1","2"]; 
// false

Then tried:

[1,2] == [1,2];
// false

and ultimately:

[] == []; 
// false

I've since found that:

"1:2".split(':').toString() == [1,2].toString();
// true

So I've solved my initial issue (kind of) but why can't arrays match each other?


Javascript arrays are objects and you can't simply use the equality operator == to understand if the content of those objects is the same. The equality operator will only test if two object are actually exactly the same instance (e.g. myObjVariable==myObjVariable, works for null and undefined too).

If you need to check if two array are equals i'd recommend to just traverse both arrays and verify that all the elements have the same value (and that the two array have the same length).

Regarding custom objects equality i'd build instead a specific equals function and i'd add it to the prototype of your class.

Considering that in the end you converted both arrays to a String and tested equality of the resulting strings, you could one day consider using a similar but more generic technique you'll find described in more than a few places:

JSON.stringify(OBJ1) === JSON.stringify(OBJ2) 

Well, don't.

While this could work if the order of the properties will always the same for those object instances, this leaves the door open for extremely nasty bugs that could be hard to track down. Always favor a more explicit approach and just write a clean and readable function that will test for equality checking all the required fields.


The == operator for Objects in Javascript only checks to see if the objects are the same actual object reference, not if they are two separate object that contain the same contents. There is no built in operator for checking if they contain the same contents. You would have to write a function to do that sort of comparison yourself.

Your string conversion is one way of comparing two arrays as long as the array elements only contain primitive values (not other objects). If the array elements could contain other elements, then you would have to make sure those objects were themselves converted into representative strings too.

And, converting to a string would not discern between an array element that contains "4" versus one that contains 4 since both convert to "4" in the string representation.


Arrays are Objects in JavaScript which are pass by reference. This means that when I initialize an array:

var array = [1, 2, 3];

I've created a reference to that array in memory. If I then say:

var otherArray = [1, 2, 3];

array and otherArray have two separate addresses in memory so they will not equal eachother (even though the values are equal.) To test out the pass by reference you can play around with arrays by doing:

var copyArray = array;

In this case, copyArray is referencing the same address in memory as array so:

copyArray === array; //will evaluate to true
array.push(4); //copyArray will now have values [1, 2, 3, 4];
copyArray.push(5); //array will have values [1, 2, 3, 4, 5];
copyArray === array; //still true

In order to test equality of values in an array or object you need to do a 'deep comparison' - for Arrays this will traverse both arrays to compare index and value to see if both are equal - for objects it will traverse every key and makes sure the value is the same. For more on deep comparisons you can check out the underscore.js annotated source:

http://underscorejs.org/docs/underscore.html


In javascript each [] is an instance of window.Array class. So you are basically trying to compare two different objects. Since array's can have any no. and any type of elements including Objects and Custom Objects and those nested arrays can again have numerous properties and arrays and so on.

It becomes ambiguous when it comes to comparison, you will never be sure what do you want to do with those objects and nested properties. So what you are trying to achieve by comparing can be done in so many other ways. You just have to figure out the right way for your case.