4 ms·
This is because: > [] + {} Becomes: > [].toString() + {}.toString() Empty array to a string is "" (empty string). Object to string is "[object Object]". >
by codepsycho 10y ago
This is because:
> [] + {}
Becomes:
> [].toString() + {}.toString()
Empty array to a string is "" (empty string). Object to string is "[object Object]".
> {} + []
Becomes:
> +[]
The prefix turns the array into a number, same as Number([]). This seems to be because {} is interpreted as a code block, not an object, due to it being the first thing.
Put it in parentheses and it'll do toString like before.