- Arithmetic operator
- Increment decrement operator
- Comparison operator
- Logical operator
- Assignment Operators
Let's take a look at how these operators are used.
We should all be familiar with this. Since primary school, we have learned arithmetic operations such as addition, subtraction, multiplication, division and remainder. The arithmetic operator is the symbol used for our arithmetic operation. It is mainly used to perform the arithmetic operation of two variables or values.
copyvar num1 = 100; var num2 = 5; // 105 console.log(num1 + num2); // 95 console.log(num1 - num2); // 500 console.log(num1 * num2); // 20 console.log(num1 / num2); // 100 % 5 = 0 console.log(num1 % num2);
However, when performing arithmetic operations, special attention should be paid to the calculation of floating-point numbers. The maximum accuracy of floating-point numbers is 17 decimal places, but the arithmetic operation is far inferior to that of integers. At the same time, we also need to pay attention not to use floating-point numbers for direct comparison.
Expressions and return values
The so-called expression is an expression composed of numbers, operators, variables, etc. And the expression can finally get a result, and the value returned to us is called the return value.
Increment decrement operator
If you want to add 1 to a variable, the previous method is as follows:
copyvar num = 1; num = num + 1;
But now you can use the increment calculator (+) to add 1 to the implementation variable. Similarly, the position of the increment calculator will also affect the final result.
If the increment operator is placed in front of the variable, the increment operator is also called the pre increment operator. If the increment operator is placed after the variable, it is called the post increment operator.
Similarly, if there is an increase, there is a decrease. And like increment, decrement operators can also be divided into pre decrement operators and post decrement operators according to their positions.
copyvar num = 10; // Pre increment ++num; console.log(num); // Post increment num++; console.log(num); // Pre decrement --num; console.log(num); // Post decrement num--; console.log(num);
If the pre and post are used separately, they achieve the same effect. The difference between the two is:
- When using leading: first add / subtract 1, and then return the result.
- Use postposition: first return the original value, and then add / subtract 1.
copyvar num = 10; console.log(num++ + 10); num = 10; console.log(++num + 10);
The comparison operator is a Boolean comparison operator, and the result of the comparison between the two tables is called the comparison operator.
Greater than or equal to
Less than or equal to
If the values are equal
Not equal to
Congruence, the value and data type are consistent
copyvar num1 = 1; var num2 = 10; var str1 = '1'; console.log(num1 > num2); console.log(num1 < num2); console.log(num1 >= num2); console.log(num1 <= num2); console.log(num1 == str1); console.log(num1 != str1); console.log(num1 === str1);
It can be noted that = = and = = = are different. When comparing, we must pay attention to the differences between them.
Logical operator is an operator used for Boolean operation, and its final return result is also a Boolean value, which is often used for conditional judgment. Common logical operators are shown in the following table.
The difference between & & and & & is that once the Boolean value before the symbol is false, the operation of the Boolean value after the symbol will continue, and then the operation result of the two Boolean values will be determined to be false. And && once the Boolean value before the symbol is false, the Boolean value after the symbol will not be calculated, and the final result of the two Boolean values will be determined to be false. This process is called short-circuit operation.
And | means that as long as any Boolean value before and after the symbol is true, the final result is true.
copy// false console.log(1 > 2 && 2 > 1) // true console.log(1 > 2 || 2 > 1) // false console.log(!true)
As the name suggests, the so-called assignment operator is the operator used to assign data to variables. The commonly used assignment operators are summarized as follows.
Assign value after adding or subtracting a number
Assignment after multiplication, division and remainder
copy// Direct assignment var num1 = 10; var num2 = 20; // 30 console.log(num1 += num2); num1 = 10; num2 = 20; // 10 console.log(num2 -= num1); num1 = 10; num2 = 20; // 200 console.log(num1 *= num2); num1 = 10; num2 = 20; // 2 console.log(num2 /= num1); num1 = 10; num2 = 20; // 10 console.log(num1 %= num2);
The priority of operators determines the execution order of operations in our expression. The higher the priority, the higher the execution order. The priority classification of common operators is shown in the following table. In general, complex expressions should be avoided as much as possible in the development process. If they are really necessary, curly braces can also be used to improve the readability of the code and prevent the error of program results caused by neglecting the execution sequence of code operations.
Logical operation method
First & & then||