Why does `{} + []` return a different result from `a = {} + []` in Javascript?

后端 未结 2 705
时光说笑
时光说笑 2020-12-19 03:33

In (at least) Firefox Web Console and JSBin, I get

> {} + []
0
> a = {} + []
\"[object Object]\"

Node.js returns \"[object Obje

2条回答
  •  囚心锁ツ
    2020-12-19 04:28

    On the browser console, when it isn't preceded by a = (or some other code that changes its context), {} is treated as a block, not an object literal.

    Since the block is empty it does nothing, leaving + [].

    The unary plus operator converts the array to a number, which is 0.

提交回复
热议问题