D
D
Denis Denis2015-10-17 12:00:55
JavaScript
Denis Denis, 2015-10-17 12:00:55

Why in javascript {} + [] returns 0 while [] + {} returns "[object Object]"?

how to read it? 270c798d8762499a8fd56eeed53a6c99.png
Thanks :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivanq, 2015-10-18
@golovewkin

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 question

Ask a Question

731 491 924 answers to any question