Implicit type conversions
A method that uses Boolean (), number (), String () to convert a data type is called a display type conversion, and it is relative to the implicit type conversion, and the implicit type conversion does not have an obvious flag, but instead the JS interpreter feels that doing such a conversion is appropriate, converting it, such as a unary plus, unary minus, increment and decrement, they can manipulate any data type, the final result all return the number, this is a good understanding, and the type conversion in the subtraction operation is more complicated and troublesome, so there may be a lot of strange phenomenon;
Multiply sex operator
including multiplication, division and modulo operation, modulo operation is to take the remainder;
When using multiplicative operations in JS:
- If the operands on both sides are numbers, then mathematical operations are performed;
- If there is one operand that is not a number, convert the operand to a number, for example
1 * ' 2 '; //'2 ' will be converted to the number 2 and then participate in the Operation
- If the operand cannot be converted to a normal number, for example
1 * ' abc '; // The ' ABC ' will be converted to Nan (do not forget that Nan is actually a number), and then perform the mathematical operation, the result is Nan
Nan and any number are evaluated as Nan;
Note the problem:
1, when it comes to decimal calculation, it must be noted that 0.1 * 0.2 is not equal to 0.02, but a very close to 0.02, if you want to do a precise calculation, it is recommended to first convert the decimal number into an integer, and then the result back to the decimal;
2, when the modulo operation, the result is not necessarily an integer, can also be a decimal, such as 1.5 1.2, the result is 0.30000000000000004;
3, the result obtained by using multiplicative operator must be a numeric type;
Subtraction operator
The subtraction operator is similar to the rule of the multiplicative operator:
- If both sides are numbers, then the subtraction operation;
- If one side is not a number, then first convert it to a number and then do the subtraction operation;
Remember that Nan and any number do operations equal to Nan, while using the subtraction operator also pay attention to the problem of decimals;
Addition operator
Addition operator can be said to be the most used in the development of an operator, is also more complex, its operational rules and multiplicative operators and subtraction operators are not the same;
There are two ways to use the addition operator:
- two numbers added;
- two concatenation of strings;
If you are unsure of the data type of the operand, you can first make an explicit conversion to it;
String (a) + string (b); // get a string Number (a) + number (b); // Get a number
Relational operators
- If you are comparing two numbers, it is a simple numeric comparison that returns a Boolean value;
- If the string is compared, the comparison is based on character encoding, ' B ' is less than ' a ', because ' B ' is smaller than ' a ';
- If an object is to participate in a relational comparison, the object is first converted to a primitive value and then compared, and the valueof method of the object is called before the ToString method is called.
- If one operand is a number, the other operand is converted to a number;
- If one operand is Nan, the result of the operation is Nan, because Nan is not equal to any number;
The return result of the relational operator is a Boolean value;
The relational operator is a two-tuple operator, so its binding is left-associative, that is, left-to-right, so there may be a problem
7 > 6 > 5;
This line of code looks as if it should return true, but it is not, JS will first calculate 7 > 6, Get True, then calculate True > 5, convert True to a number after the 1 > 5, the final result is false;
Implicit type conversions are performed in addition and relational operations, except that the addition operator is more inclined to handle the string, whereas the comparison operator is biased towards the processing of numbers; In an addition operation, if one operand is a string, the other operand is converted to a string, and in the comparison operation, If one operand is a number, the other operand is converted to a number;
It is recommended that the data type of the operands not be determined before they are explicitly converted to data type;
=== Congruent judgment
- First determine whether the type is the same, if the type is not the same directly return false, only two operands of the same type of case will be the next step;
- If the two sides are numbers, then determine whether the two values are equal, but note that in JS, Nan is not equal to Nan, it is also the only value that is not equal to itself;
- If both sides are Boolean types, then the Boolean value is judged to be the same;
- If both sides are strings, then it is possible to determine whether the encoding of the two strings is the same, so two strings that look equal may not be equal because their encodings may not be the same;
- Undefined and null are only equal to themselves because both data types have only one value;
- If two objects participate in the judgment, then the object is judged by reference, so even two seemingly identical objects participate in the judgment of the result is not equal, because this can be two independent objects, then the reference address is not the same,
== Equal judgment
The difference between it and congruent is that it will perform an implicit type conversion operation, such as null and undefined, and if they are equally judged, the result will be false, because they are of different types, but if they are judged equal, the result will be true;
- When the numbers and strings are judged equally, the strings are converted to numbers first and then compared.
- If an operand is a Boolean type, the operand is converted to a number first.
true = = ' 1 '; // convert the true to 1, then the first rule to the ' 1 ' to 1, then compare, get the result true
- When the object is judged equally, it is judged whether the reference address is the same;
When an object is made equal to the original type, the object's ValueOf method is called first, then the ToString method is called, the object is converted to the original type and then compared, but the date object is special, and it calls the ToString method to convert to the original type;
But one of the more used in daily development is the comparison between null and undefined, a comparison of numbers and strings, which converts strings into numbers first;
Not congruent (!==) and unequal (! =)
is relative to congruent and equality, and the result is in turn;
= Assign Value
The assignment operator is actually a two-dollar operator, but the difference is that the expression on the left side of it must be a variable or an object's property, that is, its left side is a value that can be changed, is also an lvalue; the expression on the right side of the assignment operator can be any data type;
var num = ten= num + 10;
In this line of code, the addition operation is performed first, and then the assignment is performed because the assignment operator has a lower precedence than the addition operator; Note that the code should not be written as Num + 10, because it is only computed, no results are saved, and it is not num++; mixed, num++; with num = num + 1 ; the results are consistent;
Compound assignment operator
That is, an assignment operation with an operator that writes an operator before an equal sign (=), typically
+=
-=
*=
/=
%=
This is just a simple notation, but the performance is not improved at all;
Continuous assignment
var a = b = 1;
This is not recommended, so B becomes a global variable, causing variable pollution;
If A and B are two objects, especially two associated objects, the problem is more complex;
———— finishing from Shahi teacher's Front end Audio tutorial # accompany you to read books # (Himalaya)
Subtraction and comparison assignment in JS