Why is javascript not equal to [1,2,3]?

Today I played with arrays in Javascript and noticed this little stone:

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

It seems rather strange to me that the array is not equal to itself.

But then I noticed this, which was even stranger:

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

?!?!?!? !!!?

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

I understand that == is not the same as === . However, what evil can Mr. Javascript cause such strange things?

+10
javascript
source share
6 answers

So, first you need to understand how javascript handles the values ​​in your program. All of your variables that you create will simply be references to the place in memory where this object is stored. Therefore, when you do this:

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

... he does three things:

  • Put an array ([1,2,3]) in a heap
  • Put another array ([1,2,3]) in the heap (note that it will have a different memory location)
  • Compare the two links. They point to different objects in different places in memory, so they are considered not equal.

You can verify the correct 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 string question

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

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

I can’t give you the whole picture, since few people understand every nuance of madness, that is, JavaScript, but hopefully this clears the fog.

+14
source share

In the first part, you create two different objects, because arrays are just objects, and since you created two of them, they are both unique.

+4
source share

== operator

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

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

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

+4
source share

The first comparison fails because a basic comparison of two objects will check if they are literally the same reference object, and not if both objects have the same value. If you want to compare two arrays, you have to iterate over the values.

 function arrayCompare(arr1, arr2) { if (arr1.length !== arr2.length) return false; for (var i = 0, len = arr1.length; i < len; i++) { if (arr1[i] !== arr2[i]) return false; } return true; } 

Just remember that this is not a recursive comparison, so it will only work with an array of primitive values.

The second comparison works because == will try to force argument types, and when you convert an array to a string, this is the result.

 [1,2,3].toString() === '1,2,3' 
+2
source share

The two Array objects are different and therefore not equal when comparing == . To compare them, you will need to loop, the index check in both is identical (and will be recursive if the elements are also Array or Object s).

Secondly, since Array implicitly called toString() , which returned '1,2,3' (try).

This is due to the fact that, according to the rules of a == (not strict) comparison in ECMAScript, the left operator was forced to convert to String ( == ) type.

+1
source share

Because == only coerces if it needs to get the same type (for example, only if the types of the operands are different). While doing

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

... coercion is not required; both are objects. This is not the same object, so it is false . People think of == as a "forced" equality operator, and it is, but the key is that it only forces if it should.

But do

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

... includes operands of different types: string and object. So the coercion is done. In this case, the object is coerced into the string, as if using String(obj) , which calls its default toString behavior, which is for .join() arrays. join by default uses "," as a delimiter, so the resulting string matches "1,2,3" . (You can find the complete logic of why an object is coerced to a string in a specification .)

+1
source share

All Articles