2

I'm trying to get a deeper understanding of JavaScript and the way it handles object and i came across a couple of strange cases that i'm not sure why i'm getting such result.

i am using the console to adding and concatenating objects together(using chrome console):

>[]+[]
""
>{}+{}
NaN
>{}+[]
0
>[]+{}
"[object Object]"

i would appreciate if somebody have a good explanation for this and/or can recommend farther reading to help me understand reason for this behavior

oqx
  • 2,157
  • 17
  • 27
  • 2
    look into toString and valueOf, and how cross-type comparisions are made – dandavis Aug 12 '14 at 19:16
  • 1
    A leading `{}` is always interpreted as a block, not as an object, so your middle two examples are simply `+{}` and `+[]` (an object and an array with a unary plus). – apsillers Aug 12 '14 at 19:26
  • thanks @apsillers the answer you have pointed is what i am looking for – oqx Aug 12 '14 at 19:30

0 Answers0