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

Nasir picture Nasir · Dec 4, 2012 · Viewed 8.1k times · Source

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 template engine. So my code looks like this:

var data = {{ series|safe }}; 
// data will be [[]] if no data is present
if (data ==[[]])
  console.log('no data');

The if always returns false. That means in [[]] == [[]] is false and my test shows that []==[] is false as well.

Any descriptions would be appreciated.

Answer

Quentin picture Quentin · Dec 4, 2012

Because == (and ===) test to see if two objects are the same object and not if they are identical objects.

Most test frameworks will include functions such as deepEqual if you want to see if two objects are identical.