0

This might be a rather silly question, but I am having some troubles with arrays in Javascript right now. Namely, why is this:

var data = new Array(new Array('a', 'b', 'c'));

not equal to this:

var data2 = [['a', 'b', 'c']];

If I compare both arrays and print out the result it says false. Why is that?

alert(data2==data);
>false

and much more confusing, if I create a third data array as data2 and name it data3, why is it still returning false when I compare both?

var data3 = [['a', 'b', 'c']];
alert(data2==data3);
>false

Am I getting something wrong here?

maRtin
  • 6,336
  • 11
  • 43
  • 66
  • 8
    You're comparing references, not values. – soktinpk Nov 24 '14 at 17:29
  • 7
    Every new array - is a new object with its own reference. That's why even `[]==[]` returns `false`. – Kiril Nov 24 '14 at 17:30
  • If you want to compare different arrays content's then have a look at this accepted answer; http://stackoverflow.com/questions/13142968/deep-comparison-of-objects-arrays – Renato Gama Nov 24 '14 at 17:31

1 Answers1

0

Here is another comprehensive answer on the subject:

How to compare arrays in JavaScript?

The basic gist of how that code implements array equality is looping through both and checking piece by piece to see if each index of the array is equal.

Community
  • 1
  • 1
Alex Mcp
  • 19,037
  • 12
  • 60
  • 93