Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If [] is [] and Array.prototype is [] why doesn't ([] == Array.prototype)

I'm messing around in console and saw the following:

>>> []
[]
>>> Array.prototype
[]
>>> [] == Array.prototype
false
>>> [] === Array.prototype
false

Can anyone explain this behavior? (Sounds like a good candidate for wtfjs)

like image 671
qwertymk Avatar asked Dec 22 '22 19:12

qwertymk


2 Answers

In Javascript, == on arrays is pointer equality, ie only true if the both arrays are the same object. If arrays aren't pointer equal, then storing to one won't affect the other.

like image 178
Raph Levien Avatar answered Dec 28 '22 11:12

Raph Levien


>>> typeof [] == typeof Array.prototype
true
like image 43
Dennis Kreminsky Avatar answered Dec 28 '22 11:12

Dennis Kreminsky