1. Add two square brackets [] []
The square brackets do not have the function of statement blocks, so the two square brackets here are an array. To add two arrays (object types), they must first be converted into value types (basic types).
1. Convert to value type and call valueOf. The valueOf() of [] is still its own
var arr = [];
arr.valueOf() === arr; // true
2, converted to string, toString of [] It is an empty string
[].toString(); / / ""
String([]) // ""
The result is out. When two empty strings are added, the result is still an empty string. That is, " " here refers to string concatenation rather than number addition.
2. Addition of curly brackets and square brackets
{} []
Note that the braces here are still not object literals, but empty statement blocks. Therefore, it can be removed, which is equivalent to
[]
Note that the two operands that appeared before have become an actual single operand. The " " operator only represents one meaning when there is only one operand: arithmetic addition. That is, there is no meaning of string concatenation here.
The toString() in square brackets is an empty string, which is equivalent to
""
" " represents the arithmetic addition operation. The string is not a number, so it is converted to a numeric type. As mentioned in the previous article, the empty string is converted to a numeric type, which is 0.
Then the final result is 0.
3. Add square brackets and curly brackets
[] {}
Compared with the above, only the order of brackets and parentheses is exchanged. The results are different. After the braces are placed on the right side, the meaning of the braces discussed above is different. The curly braces here are an object literal rather than a statement block.
The operands on both sides of " " are converted into value types: "" and "[object Object]" respectively. At this time " " means string concatenation. That is,
"" "[object Object]"
The result is "[object Object]".
4. Try adding parentheses with them too A sudden whim! Well, although parentheses have ambiguity, they cannot be used as operands.