Unary operator
An operator that can only operate one value is called a unary operator.
The unary operator is the simplest operator in ECMAScript.
1. Increment and Decrease operators
The increment and decreasing operators directly borrow from C, and each has two versions: the pre-type and the post-type. Gu Mingsi said that the pre-type should give way to the variable to be operated, while the post-type should be followed by the variable to be operated.
Front type:
var num1 = 1;var num2 = 2;var num3 = ++num1 + num2;//4
Rear type:
var num1 = 1;var num2 = 2;var num3 = num1++ + num2;//3
The above two codes have different results, because there is a very important difference between post-increment and pre-increment and decreasing, that is, post-increment and decreasing operations are performed after the statement containing them is evaluated.
Scope of application:
Pre-increment and post-increment all four operators apply to any value. When applied to different values, this operator converts the value like the Number() transformation function, and adds and subtracts 1 after conversion.
2. One-way addition and subtraction operators
The unary addition and subtraction operator is mainly used for basic arithmetic operations, and can also be used to convert data types (the operator converts this value like the Number() transformation function).
Boolean operator
There are three Boolean operators: non (NOT), and (AND), or (OR).
1. Logical non-logical
The logical non-operator is represented by an exclamation mark (!) and can be applied to any value in ECMAScript. Regardless of the data type this value is, this operator returns a Boolean value.
Using two logical non-operators at the same time will actually simulate the behavior of the Boolean() transformation function
2. Logic and
Logic and operators are represented by two sums (&&), and there are two operands and can be applied to any type of operand. Logic and short-circuit operation, that is, if the first operand is evaluated as false, then the second operand will not be evaluated.
When both values are true, the result is true. When two values are true and false, the result is false. Returns false when both values are false.
When one of the values is not a boolean: follow the following rules
The first operand is false, and the first one is returned;
When the first operand is true, the second one is returned.
If the first operand is an object, the second operand is returned
var a = {b:1};a && 'ss'//"ss"If the second operand is an object, the object will be returned only if the evaluation result of the first operand is true
'ss' && a//Object {b: 1}If both operands are objects, the second operand is returned
var c = {d:2};c && a//Object {b:1}(1) If an operand is null, return null
(2) If there is an operand that is NaN, then NaN is returned
(3) If an operand is undefined, return undefined
3. Logical or
Similar to logic and operators, logic or operators are also short-circuit operators. That is to say, if the evaluation result of the first operand is true, the evaluation of the second operand will not be performed.
(1) The first operand is true, return the first
(2) The first operand is false, return the second
Multiplication operator
ECMAScript defines 3 multiplication operators: multiplication, division and modular
Infinity*0//NaN0/0//NaNInfinity/Infinity//NaN
Additive operator
1. Addition (turn string)
Both operators are numeric values
Perform regular addition calculations.
Infinity + -Infinity//NaN
If an operand is a string
If both operators are strings, then the second operator is spliced with the first operator
If only one operator is a string, convert the other operand to a string and then splice the two strings together.
If this operand is an object, numeric or boolean, then call their toString() method to obtain the corresponding string value, and then apply the previous rules about strings. For null and undefined, the String() function is called and the strings "undefined" and "null" are obtained respectively.
2 + '' //"2"
2. Subtraction (return to the value)
If both operands are numerical
Perform a regular arithmetic subtraction operation and return the result. If an operand is NaN, the result is NaN
Infinity - Infinity//NaN
If there is an operand that is not a numeric value
If an operand is a string, a boolean, null, or undefined, the Number() function is called in the background to convert it into a numeric value, and then perform the subtraction calculation according to the previous rules. If the result of the conversion is NaN, the result of the subtraction is NaN.
If an operand is an object, the valueOf() method of the object is called to obtain the value representing the object. If the obtained value is NaN, the result of the subtraction is NaN. If the object does not have a valueOf() method, its toString() method is called and the resulting string is converted to a numeric value.
5 - true//4
Relational operators
If both operands are numeric values, perform numerical comparison
If both operands are strings, compare the character encoding values corresponding to the two strings
If one operand is a numeric value, convert another operand to a numeric value and perform a numerical comparison
var result = '23' < '3'//truevar result = '23' < 3//false
Equal operator
1. Equal and unequal
Convert first and then compare
(1) If an operand is a Boolean value, convert it to a numeric value before comparing the equality
(2) If there is an operand that is a string and another operand is a numeric value, convert it to a numeric value first
(3) If one operand is an object and the other is not, then the valueOf() method of the object is called and the obtained basic type value is compared according to the previous basic rules.
null and undefined are equal
You cannot convert null and undefined to any other value before comparing equality
If both operands are NaN, the equal operator also returns false. According to the rule, NaN is not equal to NaN
2. Congruent and Incomplete
Compare only, not convert
"55" !== 55 //true
Conditional operator
variable = boolean_expression ? true_value : false_value
Essentially, the meaning of this code is to determine what value to the variable variable based on the result of evaluating the boolean_expression. If the evaluation result is true, assign true_value to the variable; if the evaluation result is false, assign false_value to the variable variable.
Assign operator
A simple assignment operator is represented by an equal sign, and its function is to assign the value on the right to the variable on the left.
Comma operator
The comma operator is mostly used to declare multiple variables; but in addition, the comma operator is also used to assign values. When used for assignment, the comma operator always returns the last item in the expression.