JavaScript "+" traps deep understanding of _javascript skills

Source: Internet
Author: User
Tags arithmetic object object
One, two brackets added
[] + []
The brackets do not have the function of a statement block, so the two brackets here are an array. The addition of two arrays (object types) is first converted to a value type (the base type).
1, convert to value type call valueof,[] valueof () or own
Copy Code code as follows:

var arr = [];
arr.valueof () = = = Arr; True

2, turn into a string, [] ToString is an empty string
Copy Code code as follows:

[].tostring (); // ""
String ([])//""

The result came out. Two empty strings added, the result is still an empty string. The "+" here refers to the string concatenation rather than the number addition.

add two, curly braces and brackets
Copy Code code as follows:

{} + []

Note that the brace is still not an object's direct volume, but an empty statement block. So it can be removed, which is equivalent
Copy Code code as follows:

+ []

Notice that by then the seemingly two counts became the actual single op count. and the "+" operator only represents one meaning when there is only one operand: arithmetic addition. That means there's no string connection here.
The ToString () of the brackets is an empty string and is equivalent to
Copy Code code as follows:

+ ""

"+" represents arithmetic addition, a string that is not numeric, and therefore converts it to a numeric type. The empty string is turned into a numeric type. In the previous article, it was 0.
So the final result is 0.

add three, brackets and braces
Copy Code code as follows:

[] + {}

In contrast to the above, only the parentheses and parentheses are exchanged in order. The results are not the same. After the braces are placed to the right, the curly braces discussed above are different. The braces here are an object's direct volume, not a statement block.
The operands on both sides of "+" are converted to value types: "" and "[Object Object]". Then "+" means string concatenation. That
Copy Code code as follows:

"" + "[Object Object]"

The result is "[Object]".

Four, parentheses also and they add to try
Whim! Well, although the parentheses are ambiguous, they cannot be counted as a shipment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.