0

I'm trying to print results for following combinations in chrome console. But could not able to understand results. why console returns following results. Can anyone please explain this?

[] + {} ==> "[object Object]"

{} + {} ==> "[object Object][object Object]"

{} + [] ==> 0

[] + [] ==> ""
gopigoppu
  • 219
  • 2
  • 15

1 Answers1

2

If you add two objects (arrays are objects too), js will call toString for you, so

 [] + {} 

is actually:

 [].toString() + {}.toString()

Stringifying an array joins its elements, and as the array is empty it will be stringified to "". Objects are always stringified to "[Object object]". That explains everything except:

 {} + []

... and thats a mistake on your side, it will return "[Object object]"

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151