I was curious as to why Node.js has a different result when given the statement {} + []
depending on whether or not there is a semicolon. This does not occur in Firefox, JSC, or when executing a file with Node. Without a semicolon, {} + []
returns '[object Object]'
. With a semicolon, {} + [];
returns 0
. Can anyone explain this?
Asked
Active
Viewed 25 times
0

mwpuppire
- 13
- 4
-
What node version? – George Jul 26 '20 at 00:33
-
In Node.js 14.6.0, `{} + []` yields `"[object Object]"` and `{} + [];` yields `0`, not `NaN`. You’re confusing this with `{} + {};`. As to _why_, this has been explained multiple times already. It’s the difference between what is commonly referred to as the “expression context” and the “statement context”. Firefox et al. make different guesses as to which one to default to than Node. – Sebastian Simon Jul 26 '20 at 00:37
-
@George Node 14.5.0, if that makes a difference, though I've observed it in other version as well. – mwpuppire Jul 26 '20 at 00:43