JavaScript operators mainly include:
Arithmetic operators
| Operators | illustrate | example | Calculation results |
|---|---|---|---|
| + | add | y = 2+1 | y = 3 |
| - | reduce | y = 2-1 | y = 1 |
| * | take | y = 2*3 | y = 6 |
| / | Divide, return result is floating point type | y = 6/3 | y = 2 |
| % | Find the rest, return the result as a floating point type Both operands are required to be integers | y = 6%4 | y = 2 |
| ++ | Add, divided into pre-add and post-add Will be invalid for boolean and NULL | y = 2 ++y (previously added) y++ (added later) | y = 3 |
| -- | Decreasing, divided into predecreasing and postdecreasing Will be invalid for boolean and NULL | y = 2 --y (previously reduced) y-- (after reduction) | y = 1 |
For pre-add and post-add, the result after execution is all the variable plus 1. The difference is that the result returned during execution is different. See the following two examples:
The code copy is as follows:
var x = 2;
alert(++x);//Output: 3
alert(x);//Output: 3
var y = 2;
alert(y++);//Output: 2
alert(y);//Output: 3
The same is true for decreasing.
Assignment operator
The assignment operator = is used for assignment operation. The assignment operator is to assign the right value to the left variable. Set y = 6, see the table below:
| Operators | example | Equivalent to | Calculation results |
|---|---|---|---|
| = | y = 6 | � | y = 6 |
| += | y += 1 | y = y+1 | y = 7 |
| -= | y -= 1 | y = y-1 | y = 5 |
| *= | y *= 2 | y = y*2 | y = 12 |
| /= | y /= 2 | y = y/2 | y = 3 |
| %= | y %= 4 | y = y%4 | y = 2 |
Assignment operators can be used in nested:
The code copy is as follows:
y = (x = 2) + 5; //Result: x=2, y=7
Comparison operator
| Operators | illustrate | example | Calculation results |
|---|---|---|---|
| == | equal | 2 == 3 | FALSE |
| === | Constant equality (value and type need to be compared) | 2 === 2 2 === "2" | TRUE FALSE |
| != | Not equal, can also be written<> | 2 == 3 | TRUE |
| > | Greater than | 2 > 3 | FALSE |
| < | Less than | 2 < 3 | TRUE |
| >= | Greater than or equal to | 2 >= 3 | FALSE |
| <= | Less than or equal to | 2 <= 3 | TRUE |
The comparison operator can also be used for string comparisons.
Triple operator
Tripartites can be regarded as special comparison operators:
The code copy is as follows:
(expr1) ? (expr2) : (expr3)
Syntax explanation: When expr1 evaluates to TRUE, the value of the entire expression is expr2, otherwise it is expr3.
example:
The code copy is as follows:
x = 2;
y = (x == 2) ? x : 1;
alert(y);//Output: 2
This example determines whether the value of x is equal to 2. If x is equal to 2, then the value of y is equal to x (that is, equal to 2), and y is equal to 1.
To avoid errors, it is a good idea to enclose the expressions of the ternary operator in brackets.
Logical operators
| Operators | illustrate | example | Calculation results |
|---|---|---|---|
| && | Logic and (and) | x = 2; y = 6; x && y > 5 | FALSE |
| || | Logical or (or) | x = 2; y = 6; x && y > 5 | TRUE |
| ! | Logical non-logical, take the opposite side of logic | x = 2; y = 6; !(x > y) | TRUE |
String concatenation operator
The concatenation operator + is mainly used to concatenate two strings or string variables. Therefore, when using this operator for strings or string variables, they are not calculated as additively.
example:
The code copy is as follows:
x = "beijing";
y = x + "Hello!";//Result: y = "Hello beijing!"
// To add spaces between two strings, you need to insert spaces into a string:
y = x + "Hello!";//Result: y = "beijing Hello!"
When concatenating (addition) operations between strings and numbers, the numbers will be converted into strings and then concatenated (addition):
The code copy is as follows:
x = 25;
y = "I'm this year" + x + "years";//Result: y = "I'm 25 years old"