Why is {}+[] different from ({}+[])?

时间:2016-04-04 18:18:37

标签: javascript language-lawyer

I was recently alarmed to discover the following:

> {}+[]
0

> ({}+[])
"[object Object]"

> {}+[]+1
1

> ({}+[])+1
'[object Object]1'

> {}+[] == ({}+[])
false

Why does wrapping something in parenthesis change its type?

1 个答案:

答案 0 :(得分:5)

{} + [] is an empty block followed by a an array with a unary + operator, which is essentially, which is +[] which is 0,

({} + []) is a literal object plus a literal array, both get converted into strings, the string value of an object is "[object Object]" plus the string value of an empty array which is "", hence the result you see.