This is best explained visually. Imagine arr
as the following diagram. It's actually composed of 3 arrays, one referencing the other two:
arr [ 0 , 1 ]
| |
v v
['test', 1], ['test', 5]
When you did otherArr.slice(0)
, it creates a "shallow copy" - a new array but with identical contents to the original array. That means arr
and otherArr
are two separate arrays, but point to the same contents. arr[0]
is the same object as otherArr[0]
, and arr[1]
is the same object as otherArr[1]
arr [ 0 , 1 ]
| |
v v
['test', 1], ['test', 5]
^ ^
| |
otherArr [ 0 , 1 ]
Now to your questions:
When i evaluate arr === otherArr the result is FALSE.
As mentioned above, arr
and otherArr
are two different arrays, causing ===
to fail. For non-primitives, ===
checks the identity (i.e. is it the same object?). Also note that ===
and ==
are NOT structural checks (i.e. do they look the same?).
When i do the following, trying to change first array value:
otherArr[0][1] = otherArr[0][1] + 5;
it also changes the original array (arr)
arr[0][1] === otherArr[0][1] evaluates to TRUE
Back to our diagram, what you're effectively doing is changing the contents of the object both arrays are referencing (in this case, the one that's now ['test', 6]
).
arr [ 0 , 1 ]
| |
v v
['test', 6], ['test', 5]
^ ^
| |
otherArr [ 0 , 1 ]