2.9.5. Addition Operators
Addition operators (plus signs and minus signs) are usually the simplest operators. However, in ECMAScript, each addition operator has many special behaviors.
1. Addition operators:
Copy codeThe Code is as follows:
Var iResult = 1 + 2;
Console. log (iResult); // outputs 3
Special features:
The number of operations is NaN and the result is NaN.
Infinity and Infinity. The result is Infinity.
-Infinity plus-Infinity. The result is-Infinity.
Infinity plus-Infinity. The result is NaN.
If both operations are strings, connect the second string to the first string.
If only one operation number is a string, convert the other operation number to a string and the result is a string connected by two strings.
Example:
Copy codeThe Code is as follows:
Var iResult2 = NaN + 1;
Console. log (iResult2); // NaN
Var iResult3 = Infinity + Infinity;
Console. log (iResult3); // Infinity
Var iResult4 =-Infinity;
Console. log (iResult4); //-Infinity
Var iResult5 = "abc" + "bcd ";
Console. log (iResult5); // abcbcd
Var iResult6 = 5 + "5 ";
Console. log (iResult6); // 55
2. subtraction operators:
Copy codeThe Code is as follows:
Var iResult = 2-1;
Console. log (iResult); // 1
Special features:
The number of operations is NaN and the result is NaN.
Infinity Minus Infinity. The result is NaN.
-Infinity Minus Infinity. The result is NaN.
Infinity Minus Infinity. The result is Infinity.
-Infinity minus-Infinity. The result is-Infinity.
If both operations are strings, the result is NaN.
If only one operation number is a string, convert the string to a number before performing the operation.
Example:
Copy codeThe Code is as follows:
Var iResult2 = NaN-1;
Console. log (iResult2); // NaN
Var iResult3 = Infinity-Infinity;
Console. log (iResult3); // NaN
Var iResult4 =-Infinity-(-Infinity );
Console. log (iResult4); // NaN
Var iResult5 =-Infinity;
Console. log (iResult5); //-Infinity
Var iResult6 = Infinity-(-Infinity );
Console. log (iResult6); // Infinity
Var iResult7 = "abc"-"";
Console. log (iResult7); // NaN
Var iResult8 = "5"-5;
Console. log (iResult8); // 0
Var iResult9 = "a"-5;
Console. log (iResult9); // NaN
2.9.6. Relational operators
Relational operators <,>, <=,> = execute the comparison between two numbers, and return a Boolean value. If both operands are strings, the ASC codes of the two strings are compared one by one. If only one side is a string, the strings are converted to numbers for comparison. For example:
Copy codeThe Code is as follows:
Var bResult = 2 <1;
Console. log (bResult); // false
Var bResult = "B" <"";
Console. log (bResult); // true
Var bResult = "B" <"";
Console. log (bResult); // false
Var bResult = "13" <"2 ";
Console. log (bResult); // true
Var bResult = 13 <"2 ";
Console. log (bResult); // false
Var bResult =-1 <"";
Console. log (bResult); // false
In the code of Row 3, when "a" is converted to a number, NaN is returned, and false is returned for any relational operation containing NaN.
2.9.7. Equality Operators
1. equal signs and non-equal signs
In ECMAScript, equal sign (=) and non-equal sign (! =) Returns a Boolean value. To determine whether the two arithmetic operations are equal, both types are converted. The conversion rules are as follows:
If a number is a Boolean value, convert it to a numeric value before checking equality. Convert false to 0, and true to 1.
If one operation number is a string and the other operation number is a number, try to convert the string to a number before checking the equality.
If one operation is an object and the other is a string, try to convert the object to a string before checking equality.
If one operation is an object and the other is a number, convert the object to a number before checking for equality.
During comparison, the operator also follows the also column rules:
The null and undefined values are equal.
When equality is checked, null and undefined cannot be converted into other values.
If the number of operations is NaN, the equal sign returns false, and the non-equal sign returns true. Note: Even if both operations are NaN, the equal sign returns false because, according to the rule, NaN is not equal to NaN.
If both arithmetic operations are objects, the referenced values are compared. If the two arithmetic numbers are directed to the same object, the equal sign returns true, otherwise the two arithmetic numbers are not equal.
Example:
Copy codeThe Code is as follows:
Console. log (null = undefined); // true
Console. log ("NaN" = NaN); // false
Console. log (5 = NaN); // false
Console. log (NaN = NaN); // false
Console. log (NaN! = NaN); // true
Console. log (false = 0); // true
Console. log (true = 1); // true
Console. log (true = 2); // false
Console. log (undefined = 0); // false
Console. log (null = 0); // false
Console. log ("5" = 5); // true
2. Full and non-full numbers
Similar operators of equal signs and non-equal signs are full and non-full. The two operators are the same as equal signs and non-equal signs, but they do not perform type conversion before checking equality. The full equal sign is represented by three equal signs (=). The non-full equal sign is expressed by an exclamation point and two equal signs (! =) Indicates that true is returned only when the number of type conversion operations is equal. For example:
Copy codeThe Code is as follows:
Console. log ("55" = 55); // true
Console. log ("55" === 55); // false
Console. log ("55 "! = 55); // false
Console. log ("55 "! = 55); // true
2.9.8. Conditional Operators
The condition operator is the same as that in other languages: varialbe = boolean_expression? True_value: false_value;
Example:
Copy codeThe Code is as follows:
Function Max (iNum1, iNum2 ){
Return iNum1> = iNum2? INum1: iNum2;
}
Console. log (Max (1, 3); // 3
Console. log (Max (3, 1); // 3
2.9.9. Value assignment operator
A simple value assignment operator is implemented by equal sign (=). It only assigns the value on the right of the equal sign to the variable on the left of the equal sign. For example:
Var iNum = 10;
Composite value assignment is implemented by multiplication, addition, or displacement operators plus equal signs (=. These assignment operators are abbreviated to the following common situations:
Copy codeThe Code is as follows:
Var iNum = 10;
INum = iNum + 10;
// Equivalent
Var iNum = 10;
INum + = 10;
Each major arithmetic operation and other operations have a composite value assignment operator:
Multiplication/assignment (* =)
Division/assignment (/=)
Modulo/assign value (% =)
Addition/assignment (+ =)
Subtraction/assignment (-=)
Shift left/assign value (<=)
Shifts the value to the right (> =)
Unsigned shift to the right/assign a value (>>>=)
2.9.10. Comma Operator
You can use the comma operator to execute multiple operations in a statement. For example:
Var iNum = 1, iNum2 = 2, iNum3 = 3;
The comma operator is most commonly used in variable declaration.