Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't [1,2,3] equal to itself in Javascript? [duplicate]

Tags:

javascript

I was playing around with arrays in Javascript today and noticed this little gem:

alert([1, 2, 3] == [1, 2, 3]); //alerts false

It strikes me as rather odd that the array is not equal to itself.

But then I noticed this, which was even weirder:

alert([1, 2, 3] == "1,2,3");  //alerts true

?!?!?!?!!!?

Why in the world is [1, 2, 3] not == to itself but is == to the string?

I realize that == is not the same as ===. Even so, what evilness could cause Mr. Javascript do such weird things?

like image 358
Peter Olson Avatar asked Sep 06 '11 03:09

Peter Olson


People also ask

Why does [] === [] return false?

Note that === never causes type coercion, but checks for correct types first and yields false if they are not equal! Only if the types are equal, it compares the actual values. So this method of comparison is far more reliable than == .

Why are two identical objects not equal to each other?

Objects are not compared by value: two objects are not equal even if they have the same properties and values. This is true of arrays too: even if they have the same values in the same order. var a = {}; // The variable a refers to an empty object.

Why does JavaScript use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

Why are arrays not equal in JavaScript?

However, we cannot use the equality operator for comparing the arrays. The reason behind this is that JavaScript array is an object type and objects are compared based on the references of the variables and not on the values. In the above code, we have initialized two arrays that are the same.


2 Answers

Ok, so first you need to understand how javascript treats values in your program. All of your variables that you create are going to merely be references to a location in memory where that object is stored. Therefore, when you do this:

alert( [1,2,3] == [1,2,3] );

...it does three things:

  1. Place an array ([1,2,3]) onto the heap
  2. Place another array ([1,2,3]) onto the heap (notice it will have a different memory location)
  3. Compare the two references. They point to different objects in different locations in memory, thus it is considered not equal.

You can check for some sane behavior by running this code:

var a = [1,2,3];
var b = a;
alert (a == b)   // Result is true. Both point to the same object.

Now for your question about the string

When you use the == operator tries to convert the two operands to the same type (evil behavior...I know...)

When it does this, it decides to convert both to a string before it does the compare (thus the result is really "1,2,3" === "1,2,3", which evaluates to true.

I can't give you a complete picture, as there are few people who understand every nuance of the madness that is JavaScript, but hopefully this clears some of the fog.

like image 115
riwalk Avatar answered Sep 18 '22 03:09

riwalk


== operator

[..] If either operand is a string, the other operand is converted to a string if possible. [..] If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

That is to say, [1, 2, 3] converted to a string equals "1,2,3". One array object does not equal another array object though.

like image 29
deceze Avatar answered Sep 22 '22 03:09

deceze