A deep understanding of the "+" trap in JavaScript

Source: Internet
Author: User

1. Add brackets
[] + []
Brackets have no block clause, so the two brackets here are an array. To add two arrays (object types), convert them to value types (basic types ).
1. to convert the value type, call valueOf, [] valueOf () or your own.
Copy codeThe Code is as follows:
Var arr = [];
Arr. valueOf () === arr; // true

2. convert it to a string. The toString of [] is a null string.
Copy codeThe Code is as follows:
[]. ToString ();//""
String ([]) // ""

The result is displayed. The two empty strings are added, and the result is still a null string. That is, "+" indicates the string connection, not the addition of digits.

2. Add braces and brackets
Copy codeThe Code is as follows:
{} + []

Note that the braces here are still not the object's direct quantity, but empty statement blocks. Therefore, you can remove it, which is equivalent
Copy codeThe Code is as follows:
+ []

Note: In this case, the number of two operations is changed to the actual number of single operations. The "+" operator only represents one operation number: arithmetic addition. That is, there is no meaning of string connection.
The toString () in the brackets is an empty string, which is equivalent
Copy codeThe Code is as follows:
+ ""

"+" Indicates arithmetic addition. A string is not a number, so it is converted to a number. Convert a null string to a numeric type as mentioned in the previous article, which is 0.
The final result is 0.

3. Add brackets and braces
Copy codeThe Code is as follows:
[] + {}

Compared with the above, the brackets and parentheses Exchange Order. Results are different. After placing the braces on the right, they have different meanings from the braces discussed above. The braces here are a direct volume of objects rather than statement blocks.
The operation numbers on both sides of "+" are converted to value types: "" and "[object Object]". "+" Indicates a string connection. That is
Copy codeThe Code is as follows:
"" + "[Object Object]"

The result is "[object Object]".

4. Try adding parentheses with them.
Whimsy! Okay, although parentheses have many meanings, they cannot be used as the number of operations.

Related Article

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.