We talked about the data type and type conversion in JavaScript. today, let's talk about how each operator in JavaScript performs type conversion. the following describes the working mechanism of the six most commonly used operators in sequence:
Typeof
The typeof operator returns the string representation of the operand type. There are two main points to note:
● Undefined or undeclared variables will return "undefined". For example, if a is not declared, typeof a will return "undefined ".
● Typeof will "lie" when the operand is null or a function ".
In addition, the operands and corresponding type strings can be found in the following table:
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object cannot be called"
Object, which can be called "function"
I marked"⚠The typeof mentioned above is misleading: type of null should return "Null", and any function type should be "object ".
Subtraction (-) www.2cto.com
Converting the operands on both sides to numbers. "8"-true will be converted to 8-1. It is indeed very simple, but the following addition is not.
Addition (+)
Addition is the most troublesome operator in JavaScript. Let's see what happened when executing a + B:
1. The operands on both sides are first converted to the original starting value. Here we call them A and B.
2. If any of the original values is A string, convert the other value to A string, perform A connection operation between A and B, and return the connected string.
3. Otherwise, convert both A and B to numbers and return the sum of two numbers.
For example:
8 + "5" then "8" + "5" then "85"; // "5" is a string, so 8 is also converted into a string, the value after connection is "85 ″
8 + true then 8 + 1 then 9; // No string, both sides are converted to numbers, true to 1, return the sum and 9
"8" + true then "8" + "true" then "8true"; // "8" is a string. convert true to string "true ", the value after connection is "8true"
Less than (<)
Unlike the addition operator, the less than operator compares the operators as strings only when both operands are strings. The following is a formal operation procedure:
1. The operands on both sides are converted to the original starting value. Here we call it A and B.
2. If both original values are strings, compare A and B by string.
3. Otherwise, convert all of them into digits and compare them by number size.
For example:
8> "5" then 8> 5 then true; // both sides are not strings, "5" is converted to number 5
8> true then 8> 1 then true; // both sides are not strings, true is converted to number 1
"8"> "18" then true; // both sides are strings, and the 8 ascii code is greater than 1 ascii code
Strictly equal (=)
Many operators are the most worry-free, also known as the third equal sign (=). Its operation is simple: Check whether the types of the two operands are equal. If they are equal, check whether their values are equal. the sibling operator (=) is complicated.
Equal (=)
The most annoying operator in JavaScript. Its working mechanism is as follows:
1. First check the types of the two operands. if they are of the same type, continue to perform strictly equal comparison.
2. If both operands are null or undefined, true is returned.
3. If one of the operands is a string and the other is a number, convert both of them into numbers and perform strictly equal comparison.
4. If one of the operands is a Boolean value, convert it to a number and return to Step 1 for further execution.
5. If one of the operands is a string or number, and the other is an object value, convert the object value to the original value, and return to Step 1 to continue execution.
6. Return false.
This basically means that if the two operands have different types, the working mechanism of the judgment is similar to <comparison. If the types are the same, it is similar to strictly equal comparison. to sum up, when the types are different, convert both operands to the original values. Unless both original values are strings, convert the original values to numbers and then compare them, in addition, null = undefined is true.
8 = "5" then 8 = 5 then false; // "5" into a number 5
1 = true then 1 = 1 then true; // true to convert to number 1
0 = "" Success 0 = 0 success true; // "" to 0
0 = "0" then 0 = 0 then true; // "0" to 0
"" = "0" defaults false; // the string is used to determine whether it is equal.
"1000" = "1e3" defaults false; // the string is used to determine whether it is equal.
1000 = "1e3" Success true; // "1e3" is converted to 1000, scientific notation
5 =={ valueOf: function () {return 5 ;}} expected 5 == 5 then true; // convert the object value to the original value 5
These six operators are not all operators, but they must be the most troublesome.