Can anyone explain to me how the results of the following was evaluated?
{} + {} // NaN
[] + {} // "[object Object]"
{} + [] // 0
[] + [] // ""
Can anyone explain to me how the results of the following was evaluated?
{} + {} // NaN
[] + {} // "[object Object]"
{} + [] // 0
[] + [] // ""
For {}+{}, the first {} is interpreted as a block, the second {} is interpreted as an empty object and the + is interpreted as a unary plus operator, so {}+{} is equivalent to:
{
//Empty block, does nothing
}
+{} //Unary + applied to empty object, which is NaN
Similarly, in {}+[], the {} is interpreted as a block and +[] is interpreted as the unary plus operator applied to an empty array, which gives 0.
For []+{}, the + is interpreted as a string concatenation operator, so both the operands are converted to strings, in this case [] get converted to the empty string ("") and {} gets converted to "[object Object]", then both are concatenated together giving ""+"[object Object]" which is "[object Object]".
Similarly, for []+[], both arrays get converted to the empty string, giving ""+"" which is "".