# Meaning and classification

A form consisting of data itself or operational data connected by operators

Example: 1 + 1 Where 1 and 1 are two operation data

classificationdivide
Arithmetic operator+,-,*,/,%,+ +,- -
Comparison operator>, <, > =, < =, all equal (three equal signs), not all equal (! = =)
Logical operatorLogical and, logical or, logical non
Bitwise Operators Bitwise and, bitwise OR, bitwise XOR, bitwise right, bitwise left
Assignment Operators =,+=,-=,*=,/=
ternary operator Conditional expression? Expression 1: expression 2

## 1. Arithmetic operator

### 1.1 surplus

Judgment: leap year, parity, etc

```  //The first number is large and the second number is small: normal remainder
console.log(5%3); //2
//The first number is small and the second number is large: the decimal itself
console.log(5%7); //5
//Judge parity: even if divided by 2
console.log(5%2); //1 odd
console.log(4%2); //0 even
//Leap year: divide by 4
console.log(2021%4);//1 non leap year
console.log(2020%4);//0 is a leap year
//Implicit conversion
console.log('4'%3);//1
console.log('4a'%'3');//NaN
```

### 1.2 self increment

Usage: it can be self incremented only if it is a variable, which is added on the basis of the original data

++Used alone, there is no difference between front and rear

++Used in assignment, there are differences between pre and post

```  //++Use alone
var n1=2;
n1++;//You can also write + + n1;
console.log(n1);      //3

//++Use in assignment
//① Post: use first and add later
var n2=5;
var n3=n2++;
console.log(n3);     //5
//Analysis: first assign the value of n2 to n3, and then n2 performs self increment
var a='4';
a++;
console.log(a);     //5
//Analysis: first convert the value of a into a number, and then increase it by itself
var n4=5;
var n5=++n4;
console.log(n5);     //6
//Analysis: first let n4 self increment, and then assign the self increment result to n5
```

Summary:

① a=b++; First assign B to a, and then B performs auto increment;

② a=++b; First let B perform self increment, and then assign the self increment result to a

### 1.3 self reduction

Usage: it must be a variable to be self decremented. It is a downward decrement based on the original data

```  var m1=5;   //3
var m2=m1--;//5
var m3=--m1;//3
console.log(m1,m2,m3);
//Analysis: m1 is assigned to m2 first, so m2=5; Then m1 decreases from 4;
//     m1 subtracts first, so m1=3; Then m1 is assigned to m3, so m3=3
```

## 2. Comparison operator

Mainly look at the special ones

==Equal to, only compare whether the values are the same

===All equals. Compare types first and then values

!== Different types or values

### 2.1 numerical and string ratio

① The string does not contain letters

```  //Implicit conversion: converting a string to a numeric value
console.log(3>'10');//false
```

② String contains letters

```  //Implicit conversion: the string is implicitly converted to a numeric value, because the letters included are NaN
console.log(3>'10a');//false
console.log(3<'10a');//false
console.log(3 =='10a');//false
console.log(3 === '10a');//true
console.log(3 !== '10a');//true
```

Small summary: NaN only has the sum not all equal to = = = and the ratio of not equal to true. When comparing with any other operation including NaN itself, the result is false

### 2.2 string and string ratio

```//String to string ratio: Unicode code of the first character, if same, than next character
console.log('3'>'10');//true
console.log('3'>'20');//true
console.log('3'>'30');//false
console.log('3'>'40');//false
console.log('3'.charCodeAt(),'2'.charCodeAt(),'1'.charCodeAt());//51  50  49
```

## 3. Logical operator

&&Logic and (and): all truth is true

||Logical or (or): one truth and all truth

! Logical non (negate): true negates false,false negates true

### 3.1 logic and

Exercise: declare variables to save the name and password entered by the user. If the user name is root and the password is 123456, print true, otherwise print false.

```  var uname='root';
var upwd='123456';
console.log(uname ==='root' && upwd ==='123456' );//True three equals, and the type and value are the same
```

### 3.2 logic or

Exercise: declare a variable to hold the value entered by the user. If the mailbox is yu@126.com , or the mobile phone number is 12345678945, or the user name is yusang, print true, otherwise print false

```  var email='yu@126.com';
var phone='12345678965';
var uname='yusang';
console.log(email==='yu@126.com' || phone='12345678965' || uname='yusang');
//One of the three is true
```

### 3.3 logical non

Just reverse it

```  console.log(!true);//false
console.log(!false);//true
```

### 3.4 short circuit logic

&&When the first condition is false, the second condition will not be executed

||When the first condition is true, the second condition will not be executed

```  //Exercise 1: check whether the following procedures report errors
var a=5;
a>10 && console.log(b);//No error, no result, no execution
a<6 || console.log(c);//Do not execute
```
```  //Exercise 2: declare a variable using short-circuit logic, save a and an individual's age, and print an adult if it is over 18
//Print correctly
var age=61;

//Error print - short circuit logic
var age=10;
age>=18 && console.log('adult');//Not implemented, short circuit
```

Small overview

The focus of short-circuit logic is to see whether the second condition (expression) is executed, without paying attention to whether the overall result is true or false

## 4. Bitwise operator

Simulate the operation at the bottom of the computer, first convert the value of the operation to binary, then operate, and then transfer the result to decimal after the operation

### 4.1 bitwise AND

&From right to left - compare after alignment. Up and down are 1, the result is 1, otherwise 0

```console.log(3&5); //1
console.log(5&7); //5
```

### 4.2 bitwise OR

|Left to right - top to bottom alignment. Both top and bottom contain 1, the result is 1, otherwise 0

```console.log(7|10);//15
```

### 4.3 bitwise XOR

^Left to right - top to bottom alignment. Up and down are different, the result is 1, otherwise 0

```console.log(9^13);//4
```

Above analysis:

3&5    5&7    7|10    9^13
011    101      111    1001
101    111    1010    1101

001    101    1111     0100
1        5       15          4

### 4.4 shift right by position

The arrow moves according to the position. Delete the number of digits at the end. Move and delete several digits

⑤ 10 > > 1 delete the last bit of binary, i.e

1010 changes to 101, and the result is 5

② 10 > > 2 delete the last two bits of binary, i.e

1010 changes to 10 and the result is 2

### 4.5 shift left by position

Fill in 0 at the end, move several bits to fill in several 0

① 5 < < 1 move 1 bit to the left, and then fill in a 0 at the end

101 changes to 1010 and the result is 10

② 10 < < 2 bits to the left, and then fill in two zeros at the end

1010 changes to 101000, and the result is 40

## 5. Assignment operator

=+ = - = * = / =% = etc

```//Example 1
var a=1;
a+=1;//Equivalent to a=a+1;
console.log(a);

//Example 2
var str='a';
str+='b';//str=str+'b';
atr+=c;//str=str+'c';
console.log(str);//abc
```

## 6. Ternary operator

Three operands or expressions connected by two operators

Syntax: conditional expression? Expression 1: expression 2

If the conditional expression is true, execute expression 1; otherwise, execute expression 2

```  //Exercise: judge whether a person is over 18 years old. If it is an adult, otherwise it is a minor
var age=22;
console.log(res);
```

## 7. Total practice

Declare a variable to save any year. Judge whether this year is a leap year. Use short circuit logic. If it is a leap year, print 'leap year' (priority: & & > |)

```  var year=2020;
console.log(year%4===0 && year%100!==0 || year%400===0);//true
(year%4===0 && year%100!==0 || year%400===0) && console.log('leap year');//Priority & & >||
```

Keywords: Javascript

Added by jmakeig on Tue, 08 Feb 2022 05:30:26 +0200