Arithmetic operators

1.1 number operation

1.1.1 Addition, subtraction, multiplication and division

9 / 0 //Infinity
-9 / 0 //-Infinity
Copy the code

1.1.2 to take more than

-1 % 7 // 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
Copy the code

1.1.3 index

7六四屠杀3 / / 343
7六四屠杀2 / / 49
Copy the code

1.1.4 Autoincrement autodecrement

var a = 10
a++ //a increases by 1, but the expression a++ is 10, taken from the value before the increment
++a //a is incremented by 1, but the expression ++a is 11

a--// same as a++
--a// Same as ++a
Copy the code

Use autoincrement and subtraction as little as possible, use a+=1,a-=1

1.2 the string operation

Strings support only the plus sign operation

'123'+'456' / / '123456'
Copy the code

JS some weird points:

1 + '2' //'3', JS converts 1 to string and adds
3 - '2'  //1, JS converts the string to a number and subtracts it
Copy the code

Comparison operators

= = =

  • The basic type looks at whether the values are equal

  • Object to see if addresses are equal

    [] [] = = =False, the address of the first empty array is different from the address of the second empty array= = = {} {}//false
    NaN! = =NaN // Remember the exceptions
    Copy the code

Boolean operators

a = a || 100 // a is 100
console && console.log && console.log('hi') // Prevent console error

function add(n=0){
    return n+1
}// If n is null or undefined, set n to 0 (new syntax)
Copy the code

Binary operators

4.1 or operators

| as long as there is one of two binary is 1, it returns 1, otherwise it returns 0.

  (0b1111 | 0b1010).toString(2) / / '1111'
Copy the code

4.2 and operators

& If both bits are 1, the result is 1, otherwise 0

0 & 3 // 0,0 (binary 00) and 3 (binary 11) perform binary sum to get 00.
Copy the code

4.3 No operator

The binary no operator (~) changes each binary bit to the opposite value (0 to 1,1 to 0).

~ 3 / / - 4
Copy the code

Three forms of 32-bit integer is 00000000000000000000000000000011, whether the binary operation after 11111111111111111111111111111100.

Since the first digit is 1, the number is a negative number. JavaScript internally uses the form of complement to represent negative numbers, which means that you need to subtract 1 from the number, negate it again, and then add a negative sign to get the corresponding decimal value. This number minus 1 equals 11111111111111111111111111111011, get 00000000000000000000000000000100, take again the plus minus sign is – 4.

And since it’s a little bit of a hassle to do that, you can just remember that if you add something to its negative value, it’s equal to negative 1.

4.4 XOR operators

Two binary bits return 1 if they are different and 0 if they are the same.

0 ^ 3 // 3 0 (binary 00) and 3 (binary 11) perform xOR, each of which is different, so we get 11.
Copy the code

4.5 Left shift operator

The left-shift operator (<<) moves the binary value of a number to the left by a specified number of digits, terminating with 0, multiplied by 2 to the specified power. When you move to the left, the highest sign bit moves together.

4 << 1
/ / 8

-4 << 1
/ / - 8
Copy the code

4.6 Right shift operator

The right-shift operator (>>) moves the binary value of a number to the right by a specified number of digits. If it’s positive, add zeros to all the heads; If it’s negative, add 1’s to all the heads

-4 >> 1
/ / - 2
/ * / / for binary form for 11111111111111111111111111111100-4, / / moves to the right one, head to fill 1, 11111111111111111111111111111110, / / is the decimal - 2 * /
Copy the code

4.7 Right shift operator for header zeroing

The only difference between the right shift operator (>>>) and the right shift operator (>>) is that when the binary form of a number moves to the right, the head is filled with zeros regardless of the sign bit.

-4 >>> 1
/ / 2147483646
/ * / / for binary form for 11111111111111111111111111111100-4, / / take the sign bit moves to the right one, you get 01111111111111111111111111111110, // The decimal value is 2147483646. * /
Copy the code

4.7 Application of binary symbol bits

4.7.1 use ~ ~ > >, < <, > > >, | to rounded

console.log(~~ 6.83)  / / 6
console.log(6.83>>0)/ / 6
console.log(6.83<<0)/ / 6
console.log(6.83|0)/ / 6
console.log(6.83 >>>0)/ / 6
Copy the code

4.7.2 Use the and operator to judge odd and even

Use the and operator to determine if it is even

Even number &0b001 = 0Odd number &0b001 = 1
Copy the code

4.7.3 Exchange values using xOR operations

Xor operations can interchange the values of two variables

var a = 10;
var b = 99;

a ^= b, b ^= a, a ^= b;

a / / 99
b / / 10
Copy the code

Other operators

5.1 Dot operators

Grammar:

Property name = property value// To read the property value of the object
Copy the code

However, in JS you can have property values that are not objects

var a = 1 
a.toString() / / '1'

/* If the dot is not preceded by an object, JS will encapsulate it as an object. Number will become number. String will become string. Bool will become Boolean

Copy the code

5.2 void operator

Grammar:

voidExpression or statementVoid is used to evaluate expressions or execute statements, but void is always undefined
Copy the code

5.3 Comma operators

Grammar:

expression1The expression2. , expression n// take the value of the expression n as the whole value
Copy the code
let a = (1.2.3.4.5) // The value of a is 5
Copy the code

The precedence of the operator

There are 20 operators in total, summarized in MDN

  • The parentheses have the highest precedence, so there is no need to remember anything else