javascript - Why plus operator in brackets works differently? -


could please explain why results different?

  1. ({} + {}) // = "[object object][object object]"
  2. {} + {} // = 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

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -