The equality operators (===
and !==
) compare references, not values in case of objects. Mind that in JavaScript Arrays are objects. Because of that and because these are two distinct arrays (they look the same, they contain the same values, but they're not the same array) their references differ:
If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
You can read more about it on MDN.
Note: ==
and !=
equality operators also compare objects by references, but since they do more than just simple comparison (and it's often unexpected), it's generally advised not to use them and always stick to strict equality operators (===
and !==
).
How to compare arrays then?
There are at least a few different methods. Some people advise to compare JSON.stringify
of both arrays but, even though it works for simple cases, it's not really a good solution performance-wise. You can find better methods here https://stackoverflow.com/a/14853974/704894
If you happen to use some kind of utility library such as lodash
or underscore
this function is already there! See https://lodash.com/docs#isEqual