1

Can some one please let me know why an addition of an empty object and an array is a string in javascript

[ ] + [ ] = string
[ ] + { } = string
{ } + [ ] = [object Object]
{ } + { } = [object Object][object Object]

WORKING EXAMPLE

brk
  • 48,835
  • 10
  • 56
  • 78
  • 1
    You can find most of the well explained here: http://stackoverflow.com/questions/9032856/what-is-the-explanation-for-these-bizarre-javascript-behaviours-mentioned-in-the – ema Jan 18 '16 at 10:05
  • @ema you can mark this as duplicate. – Rajesh Jan 18 '16 at 10:09

1 Answers1

1

This is because + operator tries to convert to number or string, which ever is common type.

[].toString() will return "", but {}.toString() will return [object Object]

console.log([].toString())
console.log({}.toString())
Rajesh
  • 24,354
  • 5
  • 48
  • 79