JavaScript tutorial "4": operators

Operator, also known as operator, is mainly used to realize the functions of assignment, comparison and arithmetic operation. In JavaScript, there are mainly the following types of common operators:

  • Arithmetic operator
  • Increment decrement operator
  • Comparison operator
  • Logical operator
  • Assignment Operators

Let's take a look at how these operators are used.

Arithmetic operator

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.

var 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:

var 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.

var num = 10;
// Pre increment
// Post increment
// Pre decrement
// Post decrement

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.
var num = 10;
console.log(num++ + 10);
num = 10;
console.log(++num + 10);

Comparison operator

The comparison operator is a Boolean comparison operator, and the result of the comparison between the two tables is called the comparison operator.




less than


greater than


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

var 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

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.






Logic and




Logical or


Logical non

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.

// false
console.log(1 > 2 && 2 > 1)
// true
console.log(1 > 2 || 2 > 1)
// false

Assignment Operators

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.




Direct assignment


Assign value after adding or subtracting a number


Assignment after multiplication, division and remainder

// 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);

Operator priority

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.








Unary operator



Arithmetic operator

*,/,%, +,-


Relational operator

>,>= ,<,<=


Equality operator



Logical operation method

First & & then||


Assignment Operators



Comma Operator



So far, the main content of this paper is over. It mainly summarizes various operators and simple applications. Here, we have learned about variables, data types, operators, comments, input and output in JavaScript. Here we should be able to write some small demo s according to the required knowledge. Next, let's continue to look at the relevant contents of process control.

Posted by fizix on Mon, 23 May 2022 05:13:51 +0300