# 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);```
copy 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;```
copy

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
++num;
console.log(num);
// Post increment
num++;
console.log(num);
// Pre decrement
--num;
console.log(num);
// Post decrement
num--;
console.log(num);```
copy 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);```
copy ## 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.

operator

explain

<

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);```
copy 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.

operator

explain

&

And

&&

Logic and

|

or

||

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
console.log(!true)```
copy

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

operator

explain

=

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);```
copy ## 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.

priority

operator

order

1

parentheses

()

2

Unary operator

++,--,!

3

Arithmetic operator

*,/,%, +,-

4

Relational operator

>,>= ,<,<=

5

Equality operator

==,!=,===,!==

6

Logical operation method

First & & then||

7

Assignment Operators

=

8

Comma Operator

,

## summary

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