Answer the question
In order to leave comments, you need to log in
Why in javascript {} + [] returns 0 while [] + {} returns "[object Object]"?
how to read it?
Thanks :)
Answer the question
In order to leave comments, you need to log in
At the beginning of the code, JS treats {} as an empty block of code. It {} + [] == +[]
's , and +[] is 0. When the array comes first, {} is added to the empty array, and it's [] + {} = "" + {} = ({}).toString()
. An array is a different object because of this:
[].toString() == "";
+[] == 0;
+[x] == x;
+[x,y,z] == NaN; // Ой, это неправильно!
+[x,y,z] != +[x,y,z] // NaN != NaN
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question