javascript - Why plus operator in brackets works differently? -
could please explain why results different?
({} + {})
// = "[object object][object object]"{} + {}
// = nan
i understand in both cases objects converted strings, why in second case result converted number?
from:
http://www.2ality.com/2012/01/object-plus-object.html
the problem javascript interprets first {} empty code block , ignores it. nan therefore computed evaluating +{} (plus followed second {}). plus see here not binary addition operator, unary prefix operator converts operand number, in same manner number()
...
why first {} interpreted code block? because complete input parsed statement , curly braces @ beginning of statement interpreted starting code block. hence, can fix things forcing input parsed expression:
({} + {})
'[object object][object object]'
Comments
Post a Comment