1

var hits = ["a", "b", "c"];

if (hits !== ["a", "b", "c"]){
//Do some stuff here
};

Can you use the value of an array as a comparsion? The above does not seem to work for me I was wondering if this is the way to go about it or if there is another way to access the literal value of an array for comparison.

3
  • 4
    stackoverflow.com/questions/7837456/… Commented Mar 24, 2016 at 13:52
  • You could easily write a function for array comparision. Commented Mar 24, 2016 at 13:52
  • You can't compare arrays like that, because arrays are objects. And when you use comparison operator over an object, its reference will be compared and the comparison result will be returned. Commented Mar 24, 2016 at 13:52

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

so what about == and !=? wont they compare references?
So why does 'a' === 'a' return true?
@RajaprabhuAravindasamy Yes, they will. I added this to my answer.
@Tresdin as I said, in case of objects equality operators compare them by references. Primitives such as strings are compared by value. Read more about this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@Michal Miszczyszyn Out of curiosity what is the reference value of ["a", "b", "c"] in the if statement?
|
0

You can write this

if (JSON.stringify(hits) === JSON.stringify(["a", "b", "c"])) {

Comments

0

You can't do it this way because of the way Javascript handles arrays - it's not checking the values are the same, it's checking that the object is the same object (which it isn't).

2 Comments

This is actually not a good idea. What if you had something like this in an array? ['a,b', 'b', 'c'] and in a second one something like this: ['a', 'b,b', 'c']? It would return true, but those are not same arrays.
Hmm, yes of course - I will remove the suggested code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.