1.{}+[]
gives 0
2.[]+{}
gives [object object]
I know how 1 and 2 comes but
3.{}+[]+{}
gives [object object][object object]
how shouldn't it be 0[object object]
?
1.{}+[]
gives 0
2.[]+{}
gives [object object]
I know how 1 and 2 comes but
3.{}+[]+{}
gives [object object][object object]
how shouldn't it be 0[object object]
?
When you use console.log
, it executes toString
- which when using {} + []
, gives [object Object]
.
console.log({} + []);
(Note: simply placing {} + []
into the Developer Tools console gives 0
, because toString
is not called).
That's why this yields [object Object][object Object]
:
console.log({} + [] + {});
Because you're adding an object (formed from {} + []
) to another object, and because you're using the +
concatenation operator, this forces toString
- therefore, you're adding [objectObject]
and [object Object]
, yielding [object Object][object Object]
.
Note: The above all are on Chrome. In Firefox and potentially others, they work differently, which is why some things in your question are different.