The JavaScript operators mainly include:
- Arithmetic operators
- Assignment operator
- Comparison operators
- Ternary operator
- logical operators
- string concatenation operator
Arithmetic operators
operator |
Description |
Example |
Operation Result |
+ |
Add |
y = 2+1 |
y = 3 |
- |
Reducing |
y = 2-1 |
y = 1 |
* |
By |
y = 2*3 |
y = 6 |
/ |
In addition, the return result is a floating-point type |
y = 6/3 |
y = 2 |
% |
Remainder, return result is a floating-point type Requires both operands to be integers |
y = 6%4 |
y = 2 |
++ |
Accumulator, divided into front plus and back add will not be valid for Boolean values and NULL |
y = 2 ++y (ex-plus) y++ (after addition) |
y = 3 |
-- |
Descending, divided into before and after descending will not be valid for Boolean values and NULL |
y = 2 --y (ex-minus) y--(after minus) |
y = 1 |
For both before and after, the result of the execution is variable plus 1, the difference is that the result of the return is not the same when executed, refer to the following two examples:
Copy Code code as follows:
var x = 2;
alert (++x); Output: 3
alert (x); Output: 3
var y = 2;
alert (y++); Output: 2
alert (y); Output: 3
Diminishing empathy.
Assignment operator
Assignment operators = for assignment operations, the assignment operator is to assign the value on the right to the left variable. Set y = 6, see the following table:
operator |
Example |
equivalent to |
Operation Result |
= |
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 operations nesting using
Assignment operators can be nested using:
Copy Code code as follows:
y = (x = 2) + 5; Result: x=2,y=7
Comparison operators
operator |
Description |
Example |
Operation Result |
== |
Equals |
2 = 3 |
FALSE |
=== |
Constant equals (both values and types have to be compared) |
2 = = 2 2 = = "2" |
TRUE FALSE |
!= |
Not equal, can also write <> |
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 |
Comparison operators can also be used for string comparisons.
Ternary operator
Ternary can be regarded as a special comparison operator:
Copy Code code as follows:
(EXPR1)? (EXPR2): (EXPR3)
Syntax Explanation: The value of the entire expression is EXPR2 when Expr1 evaluates to TRUE, or EXPR3.
Example:
Copy Code code as follows:
x = 2;
y = (x = = 2)? X:1;
alert (y); Output: 2
This example determines whether the value of X equals 2, and if X equals 2, then the value of y equals X (that is, 2), whereas Y equals 1.
Tips
In order to avoid errors, it is a good idea to enclose the ternary operator expressions in parentheses.
logical operators
operator |
Description |
Example |
Operation Result |
&& |
Logic and (and) |
x = 2; y = 6; X && y > 5 |
FALSE |
|| |
Logical OR (OR) |
x = 2; y = 6; X && y > 5 |
TRUE |
! |
Logic is not, take the opposite of logic |
x = 2; y = 6; ! (x > Y) |
TRUE |
string concatenation operator
The connection operator + is primarily used to connect two strings or string variables. Therefore, when using the operator for a string or string variable, they are not additive calculations.
Example:
Copy Code code as follows:
x = "Beijing";
y = x + "Hello!" "; Result: y = "Beijing Hello!" "
To add a space between two strings, you need to insert a space into a string:
y = x + "Hello!" "; Result: y = "Beijing Hello!" "
When a concatenation (addition) operation is made on a string and a number, the number is first converted to a string and then concatenated (added):
Copy Code code as follows:
x = 25;
y = "I am this year" + x + "old"; Result: y = "I am 25 years old"