When using the operator against objects, the javascript interpreter must use the values ββfor the primitive using the valueOf method, which actually uses the internal ToPrimitive function to relay the type of the object to the internal method [[DefaultValue]].
Your example with a plus operator is a bit more complicated, because an operator can act like adding math or concatenating strings. In this case, it combines string representations of objects.
What really happens behind the scenes:
a = {}.valueOf().toString() + [].valueOf().toString();
Since the array is empty, the toString method returns an empty string, so the correct result should be [object Object], which is the return value from object.valueOf () toString ().
user2585506
source share