Brief introduction
JavaScript is a great language. I liked its flexibility: just the way you like to do things: change the type of a variable dynamically add methods or properties to objects, using operators of different types of variables, and so on.
However, it pays to dynamic, developers need to know how to handle different types of conversion operator: the plus sign (+), the equal sign ( ==
and ===
(!! = And ==)), inequality sign etc., many operators are own handling type conversion.
Addition operator
The most commonly used operators: +, this operator for a string or series of digital sum:
String concatenation :
var result = "Hello, " + "World!";
// 字符串 + 字符串 = 字符串 (连接)
// "Hello, World!"
Digital arithmetic sum of :
var result = 10 + 5;
// 数字 + 数字 = 数字 (相加)
// 15
JavaScript allows the use of objects, arrays, null, or undefined as an operand. The following general rules try to uncover the conversion.
Conversion rules
Using the following equation JavaScript look at how the type of conversion operation in the operator:
- If at least one operand is the object, it will be converted into the original value (string, number, or Boolean);
- After the conversion, if at least one is a string type operand, the second operand is converted into a string, and performs connection.
- In other cases, two operands are converted to digital and performs an arithmetic addition.
If two operands are primitive types, the operator checks whether at least one character string type, if the connecting operation is executed. Other cases are converted to digital and to seek together.
Object type into primitive type
Object type conversion to the original type of
- If the object type is Date, it will call the object's toString ();
- In other cases, if valueOf () returns a primitive type, it will call the object's valueOf ();
- In other cases (if valueOf () does not exist or did not return to the original type), calls the toString () method, in most cases this conversion with.
When the array is converted to the original type, JavaScript uses its join ( ',') method, for example, [1,5,6] is "1,5,6." Common JavaScript on the original image type is {} "[object Object]".
Examples of learning
The following example helps us to understand simple and complex transformation scene.
Example 1: numeric and string
var result = 1 + "5"; // "15"
Resolution:
- 1+ "5" (second operand is a string, rule number 1 2 becomes "1")
- "1" + "5" (string concatenation)
- “15”
The second operand is a string, the first operand from a digital conversion to a string, and then connect.
Example 2: Array and digital
var result = [1, 3, 5] + 1; //"1,3,51"
Resolution:
- [1, 3, 5] + 1 (Usage Rule 1, the array [1, 3, 5] is converted to the original value: "1,3,5")
- "1,3,5" + 1 (usage rules, number 1 is converted to the character string "1")
- "1,3,5" + "1" (string concatenation)
- “1,3,51”
The first operand is an array, it is converted into a value of the original string, the string is converted to digital data in the next operation, and then complete the connection of two strings.
Example 3: Digital and Boolean
var result = 10 + true; //11
Resolution:
- 10 + true (Rule 3 based on the Boolean true converted into digital 1)
- 10 + 1 (two numbers evaluated)
- 11
Since neither operand is a string, Boolean value into a digital, then performs arithmetic summation.
Example 4: Digital Object and
var result = 15 + {}; // "15[object Object]"
Resolution:
- "+ {15}" (operand is a second object, the application rule to convert a primitive object type string "[object Object]")
- 15 + "[object Object]" (Rule 2 using the digital string 15 is converted into "15")
- "15" + "[object Object]" (string concatenation)
- “15[object Object]”
The second target operand to a string value, because valueOf () method returns the object itself, it is not the original value, toString () method is called and returns a string, the second operand is a string of now, Thus figures are converted to strings, strings connecting the two final implementation.
Example 5: Digital and null
var result = 8 + null; // 8
Resolution:
- 8 + null (because neither operand is a string, the null rule-based to digital 0 3)
- 8 + 0 (digital sum)
- 8
Because the operand is not a subject or a string, null is converted to digital, and then calculates the numbers.
Example 6: null strings and
var result = "queen" + null; // "queennull"
Resolution:
- "Queen" + null (because the first operand is a string, the null basis Rule 2 translated into strings "null")
- "Queen" + "null" (string concatenation)
- “queennull”
Because the first operand is a string, null transformed into a string and connecting the string.
Example 7: undefined number and
var result = 12 + undefined; // NaN
Resolution:
- 12 + undefined (because the operand is not an object or a string, rule-based 3 to digital undefined NaN)
- 12 + NaN (digital sum)
- NaN
Since the operands are not objects or string, undefined to digital: NaN, NaN, and for the digital sum value is equal to find NaN.
Programming sixth year, to share with you some of the learning method, combat development need to pay attention to detail. 767-273-102 autumn dress. From the zero-based front-end to learn how to start. To see how the predecessors proudly forward in the programming world! Constantly updated with the latest tutorials and learning methods (web front-end system to learn the route, detailed front-end project combat instructional videos), I had wanted to learn web front-end, or change jobs, or college students, as well as work want to upgrade their skills, is studying small partners are welcome to join. We will walk together with the front tip of the tip
to sum up
To avoid potential problems, do not object to the use of the addition operator, unless you clearly define the toString () or valueOf () method. As can be seen in the example, there are many addition operator specific situations. Know the exact conversion scenarios can help you avoid future accidents, I wish happy learning!