This article provides a detailed summary of the operators in javascript, including common arithmetic operators, comparison operators, and logical operators. Very clear. if you need it, you can refer to it. In JavaScript, common operators include arithmetic operators, comparison operators, and logical operators.
Table 1 Common JavaScript operators
| Arithmetic operators |
Description |
Example |
Result |
| = |
Value assignment operator. Assign the value of the variable on the right of the operator to the variable on the left. |
X = 5; |
- |
| + |
Plus sign. Add the two data. |
Y = 1 + 2; |
Y = 3 |
| - |
Minus sign. Subtract the two data. |
Z = x-y; |
Z = 2 |
| * |
Multiplication number. Multiply two pieces of data. |
A = x * y; |
A = 15 |
| / |
Division number. Divide the two data. |
B = x/z; |
B = 2.5 |
| % |
Perform the remainder operation. Calculate the remainder of the two data division. |
C = x % z; |
C = 1 |
| ++ |
Auto-increment. Add the operand to 1. |
M = ++ x; |
M = 6 x = 6 |
| -- |
Auto-subtraction. Subtract 1 from the operand. |
N = -- x; |
N = 5 x = 5 |
| Comparison Operators |
Description |
Example |
Result |
| = |
Equal. If the two data values are equal, true is returned; otherwise, false is returned. |
Boolean1 = (x = 5 ); |
Boolean1 = true |
| ! = |
Not equal. If the two data types are not equal, true is returned; otherwise, false is returned. |
Boolean2 = (x! = 5 ); |
Boolean2 = false; |
| > |
Greater. If the left data is greater than the right data, true is returned; otherwise, false is returned. |
Boolean4 = (x> y ); |
Boolean4 = true |
| < |
Less. If the left data is smaller than the right data, true is returned; otherwise, false is returned. |
Boolean5 = (x |
Boolean5 = false |
| > = |
Greater than or equal. If the left data is greater than or equal to the right data, true is returned; otherwise, false is returned. |
Boolean6 = (x> = y ); |
Boolean6 = true |
| <= |
Less than or equal. If the left data is less than or equal to the right data, true is returned; otherwise, false is returned. |
Boolean7 = (x <= y ); |
Boolean7 = false |
| Logical operators |
Description |
Example |
Result |
| && |
Logic and. If the operands on both sides of the symbol are true, true is returned; otherwise, false is returned. |
Boolean_a = true & false; |
Boolean_a = false |
| | |
Logic or. If the operands on both sides of the symbol are false, false is returned; otherwise, true is returned. |
Boolean_ B = true | false; |
Boolean_ B = true |
| ! |
Non-logical. If the right operand of the symbol is true, false is returned; otherwise, true is returned. |
Boolean_c =! True; |
Boolean_c = false |
"+" Can also be used to connect strings
"+" Can not only add two data, but also be used to connect strings.
For example:
The code is as follows:
Var name = "Tom ";
Var age = 22;
Var person = "My name is" + name + "! I'm "+ age + "! ";
Alert (person );
Save and run the code to display My name is Tom! I'm 22!
In the above example, there are strings and numbers. When the string and numeric values are mixed, JavaScript will automatically determine the role of the "+" number, whether it is an addition operation or a connection string. If it is a connection string, the value is also converted to a string.
Discussion on auto-addition (++) and auto-subtraction (--)
It is worth noting that the plus (++) and minus (--) operators place before and after the operands have different meanings. Place it in front of the operand (pre-auto-increment/pre-auto-increment), first add 1 (minus 1) to the operand, and then perform the operation; place it behind the operand (post-auto-increment/post-auto-subtract), perform operations first, and then add 1 (minus 1) to the operand ).
For example:
The code is as follows: