Javascript data type conversion [] + {}and {} + []

Source: Internet
Author: User

The javascript data type is really amazing.

After reading a page titled A few JS questions, I did click it with interest. However, after reading those questions, I feel that I have a lack of understanding of the javascript language.

{} + [] =?

[] + 0 =?

{} + 0 =?

({} + []) =?

When I read the introduction of the Data Type in javascript advanced programming, I know that the javascript data type will appear during the operator.ConversionWhen the binary operator "+" is added, it follows some rules:

1. If both operators are numerical values, General addition operations are performed.
2. If either of the two operations is a string (1) and both operations are strings, concatenate the two operations (2). If one of the operations is strings, convert the other operations to strings, then splice
3. If an operand is an object, a value, or a Boolean value, toString () is called to convert it into a String. For undefined and null, String () is called () but what is the result of the above execution? At that time, I immediately opened the chrome console, knocked in the above Code, and looked at it, unexpectedly. {} + [] = 0[] + 0 = "0"{} + 0 = 0({} + []) = [Object Object]The cause of the above results for the javascript parser, we need to understand the use of the first {} symbol, if a variable is defined using var and {} is assigned to that variable, it is equivalent to new Object (). However, if {} is used directly in the js file, the javascript parser treats it as a statement block (although js does not have block-level scope, it is no longer an object. The second is the use of the unary operator "+". Its conversion to the data type is equivalent to calling the Number () constructor. It also has a series of rules for the Boolean type, true and false are converted to 1, 0 null to 0, and undefined to NaN string respectively. The conversion is complex.

(1) If they are all numbers, convert them to decimal, and ignore the leading zero.

(2) if it contains a valid decimal point, it will be converted to the floating point type.

(3) if it contains a valid hexadecimal format, it will be converted to a hexadecimal value.

(4) If it is a null string, it is converted to 0.

(5) convert the rest to NaN

If it is an object

(1) first call the valueOf method of the object. If the method returns the value of the original type (numeric value, string, and Boolean value), The Number method is directly used for the value, do not proceed with subsequent steps;

(2) If the valueOf method returns a value of the composite type, call the toString method of the object itself. If the toString method returns the value of the original type, use the Number method for this value, do not proceed with subsequent steps;

(3) If the toString method returns a composite value, an error is returned.

{} + [] =? Result: 0
Cause: The js parser uses the previous {} as the statement block for execution. {} + []It is equivalent to executing + []And + [] is equivalent to execution Number ([])It first calls []. valueOf () to check whether the original array (composite element) is returned. Therefore, run []. toString () to obtain ""Indicates an empty string, So Number ([]) is equivalent to executing Number (""), and the final result is 0. [] + 0 =? Result: "0"
Cause: We know that [] will execute []. toString () is changed to "". Note that it has now become a string connection. The binary operator + is changed to a string connector, so the value 0 is changed to the string "0 ", then connect to "" {} + 0 =? Result: 0
Cause: Same as the first {} + [] ({} + []) =? Result: "[Object Object]"
Cause() There is a semantic force expression operation, so the current {} cannot be regarded as a statement block and can only be an object, so {}. after toString is executed, it is [object Object], and [] is added. toString () is "", so it is the ambiguity of "[object Object]" reference Javascript parentheses "()"
Data type conversion (Ruan Yifeng)

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.