Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is an identical array not equivalent?

e.g.

$  node
> [1, 2, 3] == [1, 2, 3]
false

Apologies if I am using "identical" and "equivalent" incorrectly.

console.log([1, 2, 3] == [1, 2, 3]);

I ask, because I am used to languages such as Ruby

$  irb
irb(main):001:0> [1,2,3] == [1,2,3]
=> true

...or Python

$  python
>>> [1,2,3] == [1,2,3]
True

... where the double equals is comparing the value of the expression

like image 683
MmmHmm Avatar asked Dec 23 '22 15:12

MmmHmm


1 Answers

Because while they may have the same content, they don't point to the same reference in memory.

For more precision, imagine the following example

let arr1 = [1,2,3]
let arr2 = [1,2,3]
arr1 == arr2 //false

it makes sense, because the two arrays are different objects, even if they have similar values, for example if we then did

arr1.push(4)

only arr1 would change.

If you are looking for a solution to comparing arrays, you might find this thread useful.

like image 157
TheCog19 Avatar answered Dec 26 '22 05:12

TheCog19