{} + [] - why it is 0 or [object Object]?

后端 未结 4 870
情深已故
情深已故 2020-12-31 04:45

If you open a JS Console in your browser (in my case Chrome) and type:

{} + []

you will get 0, but when you type

console.lo         


        
4条回答
  •  囚心锁ツ
    2020-12-31 05:08

    it's because {} is an object notation so whatever you concatenate with {} it will give you an [object Object]. I your case [] is an empty so it just shows an [object Object]. so if you could try the following you will got what you want.

     console.log({} + 5);
     [object Object]5  ///console shows 
          //And
    console.log({} + "test");
    VM65:1 [object Object]test //console shows
    

提交回复
热议问题