I’m playing with Rhino and noticed this strange behavior in what appears to be operator precedence:
js> {}+{}
NaN
js> ''+{}+{}
[object Object][object Object]
js> ''+({}+{})
[object Object][object Object]
I would expect the expression ''+({}+{}) to evaluate to "NaN", since {}+{} should be evaluated first, yet this does not happen. I tested this out in V8 and Firefox and got the same result.
Why does it appear that Javascript does not follow the standard precedence given to parenthesis in this case (4*(1+2) is still the expected 12)?
A plain
{}is treated as a block statement so your code is actually:I should explain what plain means:
It means
{}in a statement context.({})is an object because parentheses cannot contain statements, they can only contain expressions.