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)
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.
riwalk
source share