Arithmetic operator

Number operation

subtracting

Division will give you a decimal

Remainder x % 7

Abnormal condition, -1%7 === -1, different from our cognition before, because JS extracts negative sign directly, 1%7 is 1, so plus negative sign, it is -1 namely. It doesn’t exist in mathematics, but you can imagine that minus 1 percent 7 is the same thing as 6 percent 7, and the remainder should be 6.

The exponent x ** 3

X ++ / ++x/x– / –x

A ++ takes the first value of a, plus +a takes the last value of a. (The value of a is 2, but the increment is different.)

var a = 10

b = a++
Copy the code

And the answer is b is equal to 10,a ++ is equal to 10,a is equal to 11.

A — take the first value of a, take the last value of a. (Both values of A are 4, but the two decrement results are different.)

Evaluate operator +x

Is evaluated

The negative number operator -x

Unlike evaluation operators, negative operators invert the sign of a value

The string operations

Strings support only addition

The following operations are wrong, but JS still gives “results”.

Comparison operator

>

Is greater than

<

Less than

> =

Greater than or equal to

< =

Less than or equal to

= =

JS Trinity diagram

0 == [], 0 == ‘0’, 0 == ‘\t’, 0 == ‘\t’

Don’t use ==, use === instead. The problem with == is that it’s always smart-aleck (automatic type conversion).

X == y truth table

[] == false but not falsy, [] == false but not {}, [[]] == false.

X === y truth table

There is no mystery. Basic types look at value equality and objects look at address equality.

[]! = = [] {}! = = []

Special case NaN! == NaN

! =

= = =

! = =

Boolean operator

Or and not

| | or

&& and

! non

Short circuit logic

Console. log && console.log(‘hi’) defensive programming in case console does not exist.

A = a | | 100 a guaranteed value, there is a a, does not exist for 100. But there’s a loophole, because all five falsy values can make a false.

To avoid this problem, use the following code:

Binary operators

Or, and, no

|

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

I don’t get the right result because I didn’t convert to binary

&

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

~

Invert a binary bit (0 becomes 1,1 becomes 0)

~3 / / - 4
Copy the code

The above expression applies binary no to 3 to get -4. This result occurs because when bitwise operations are performed, JavaScript internally converts all operators to 32-bit binary integers.

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.

Exclusive or

^

If the two bits are the same, the result is 0, otherwise, 1

Left moves to the right

The < < and > >

Now I have more than one, so if I move to the right, I’m going to get eaten, so it’s going to be0b0001. Moving to the left will complete it.

The 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. So, the operation always gets a positive value. For positive numbers, the result of this operation is exactly the same as the right-shift operator (>>), except for negative numbers.

4 >>> 1
/ / 2

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

This operation actually converts a value to a 32-bit unsigned integer.

The fastest way to see how a negative integer is stored inside a computer is to use this operator.

-1 >>> 0 / / 4294967295
Copy the code

Code above, said – 1 as a 32-bit integer, the storage form of internal use unsigned integer format, value is 4294967295 (namely (2 ^ 32) – 1, equal to 11111111111111111111111111111111).

Some use

Use the and operator to determine odd and even
Even number &1 = 0Odd number &1 = 1
Copy the code
Use ~, > >, < < > > >, | to integer
console.log(~~ 6.83)	/ / 6
console.log(6.93 >> 0)	/ / 6
console.log(6.83 << 0)	/ / 6
console.log(6.83 | 0)	/ / 6
console.log(6.83 >>> 0)	/ / 6
Copy the code
use^Swap the values of A and b
var a = 5
var b = 8
a ^= b	// A = a ^ b
b ^= a
a ^= b
console.log(a)	/ / 8
console.log(b)	/ / 5
Copy the code

Other operators

Some operators

grammar
Property name = property value// Can only be used on objects
Copy the code
role

Reads the property value of an object

doubt

Why can we have properties that are not objects?

'a-b-c'.split(The '-')
Copy the code

JS has special logic, the dot is not an object, it is wrapped as an object

The number will become the number object

A string becomes a string

Bool becomes Boolean

Example: when the number a uses the dot operator

When toString is called, JS first turns A into an object with a method, encapsulating object A1.

Get the toString that encapsulates object A1.

Kill encapsulation object A1.

Void operator.

grammar
voidExpression or statementCopy the code
role

Evaluate an expression or execute a statement

And void is always undefined

demand

<a href="http://example.com" onclick="f(); return false;" > click < / a >

Return false values organize default actions

<a href="javascript: void(f())">

Use void to show off.

Comma operator

grammar
expression1The expression2. , expression nCopy the code
role

Take the value of expression n as the value of the whole

use

Let a = (1, 2, 3, 4, 5)

So the value of A is going to be 5.

Let f = (x) = (console.log(' square = '), x * x)

Note that parentheses cannot be omitted.

Operator priority

What are the priorities

Different operators
1 + 2 * 3	/ / return 7
// Multiply and divide first, then add and subtract
Copy the code
Identical operator

From left to right a plus b plus c

From right to left a = B = C = D

Priority is what comes first, right

Specific rules

Note: parentheses have the highest priority!