Adding arrays and objects to javascript results in an error

I get these outputs when the following operations are performed

var t = {} + {}; // Alerts [object Object][object Object] t = {} + 1 //Alerts [object Object]1 t = {} + "hello" //Alerts [object Object]hello t = {} + function(){} //Alerts [object Object]function(){} t = {} + [] //Alerts [object Object] 

In the latter case, he warns [Object object] not to display [object Object][object Object] for this too?

Tested in Firefox12.0.

+4
source share
1 answer

No, because the second part of the output is the gated form of an empty array [] . Arrays are gated as a list of string values, separated by commas, so an empty array builds on an empty string. You can confirm this with console.log([] + "" === "") .

Therefore, {} + [] yields the equivalent of "[object Object]" + "" .

+7
source

Source: https://habr.com/ru/post/1412951/


All Articles