javascript - Inconsistency in JS console when adding arrays and objects -
i don't want ask why [] + [] gives empty string, or on, can't change it. , reason design of language.
my question following inconsistency, noticed in chrome , ff firebug js consoles:
{} + []; // outputs 0 console.log({} + []); // outputs [object object] var c = {} + []; console.log(c); // outputs [object object]
i understand expression returns value, , see in console output. why return value changes, when assigned variable or output using console.log()
?
does mean mentioned consoles buggy?
is there better explanation stating this way it's done?
in first example, block, equivalent running code (the plus in front of array literal converts number):
{}; +[]; // 0
however, in console.log code, considered expression, in fact object literal. can pass expressions functions, same.
function a(b) {return b;} a({}+[]); // "[object object]"
the addition operation converts them string, that's why "[object object]"
.
Comments
Post a Comment