0

Recently I came across an interesting snippet of JavaScript.

 {} + [] = 0  

Oops and the result is 0. How is this addition of objects being carried out? What's going behind the scenes? Does anyone have any idea?

guypursey
  • 3,114
  • 3
  • 24
  • 42
selvagsz
  • 3,852
  • 1
  • 24
  • 34
  • 3
    and you'll be surprised what []+{} does ... actually , if you do var a={} , then a+[] you get a different result from direclty adding {}+[] . – mpm Feb 16 '13 at 10:14
  • 1
    exact duplicate of [Problems with adding object with array](http://stackoverflow.com/questions/10376485/problems-with-adding-object-with-array) or better http://stackoverflow.com/questions/9032856/what-is-the-explanation-for-these-bizarre-javascript-behaviours-mentioned-in-the – Bergi Feb 16 '13 at 11:20
  • 1
    see also duplicate [Objects and arrays addition](http://stackoverflow.com/questions/11930644/objects-and-arrays-addition) – Bergi Feb 16 '13 at 11:21
  • @Bergi +1 for that [second link](http://stackoverflow.com/questions/9032856/what-is-the-explanation-for-these-bizarre-javascript-behaviours-mentioned-in-the) (in your first comment). Very useful. – guypursey Feb 16 '13 at 12:49

2 Answers2

0

if you are useing chrome to execute the code, if so, V8 dynamically creates hidden classes behind the scenes v8

  • 1
    I dont think it is chrome related , there is some typecasting involved , the question is what is casted into what. – mpm Feb 16 '13 at 10:31
0

{} is equal to new Object() and [] is equal to new Array() but this is for declaration. When i addition {} + [] this, i am geting string "[object object]" and length equals to 15.Aslo i am using chrome.

Kris Georgiev
  • 140
  • 1
  • 8