# Front end - JS based operator

2022-01-26 22:14:53

# Operator

## Arithmetic operator

` Add (+) reduce (-) ride (*) except (/) Remainder (%) chengfang (**)`

``````//  Addition operation
let a = 1
let b = a + 2 //  here b be equal to 3

//  reduce 、 ride 、 Same reason for division
``````

Focus on the remainder operation

The remainder operation is literally , Take the remainder of the division operation , Sometimes it's also called modular operation

``````//  for example ：5  Divide  3  Shang Wei 1  Remainder is 2
let a = 5 % 3 //  here a What's stored is 5 Divide 3 The remainder of 2

let b = 6 % 2 //  At this point, you can divide , So the remainder is 0, be b The value of is 0

//  The remainder of a small module
//  for example ：3  Divide  5, Because the divisor is smaller than the divisor , So the quotient is 0, Remainder is 3
let c = 3 % 5 //  here  c = 3

let d = 2 % 10 // d = 2
//  therefore ： Small value to large value remainder operation , The remainder is a small value
``````

Then let's look at the power operation , The operation of power is to calculate a number `n` Result of the power

``````//  for example ： Calculation 5 The square of
let a = 5 ** 2 //  Equate to  a = 5 * 5 ==> a = 25

let b = 4 ** 4 // b = 256

let c = 2 ** 3 // c = 8
``````

## Assignment operator

` be equal to (=) Add is equal to (+=) Minus is equal to (-=) Multiply is equal to (*=) Divide equal to (/=) Take remainder equal to (%=) Power equals (**=)`

It seems that although there are many , But it's easier to understand

You usually see the equal sign , First calculate the right side of the equal sign

``````//  be equal to   Both of the following are equal operations , Simply speaking   Is to assign a value to a variable
let a = 1
let b = 1 + 3

//  The following ones with operators are the same principle
//  for example : +=
let a = 1
a += 1 //  here a=2, because a += 1 Equivalent to  a = a + 1

//  Others in the same way
``````

Two special assignment operators ` Self increasing 1(++)、 Self reduction 1(--)`

``````let a = 1
a++ //  here a = 2, because a Self increasing of 1 operation , It can be understood as  a += 1, That is to say  a = a + 1

//  Empathy
let b = 3
b-- //  here b = 2, because b Self subtraction of 1 operation , It can be understood as  b -= 1, That is to say  b = b - 1
``````

So self increasing and self decreasing , Is based on its original value , Add 1 Or minus 1 Calculation , And will change its value , It has the effect of assignment operation

It should be noted that , Self increasing and self decreasing still need to pay attention to , Take self increasing as an example

``````let a = 1

let b = a++ //  Please note that , here  b = 1
``````

The reason is self increasing and self decreasing , There are two situations , One is post auto increase or post auto decrease , The other is pre self increasing and pre self decreasing , The writing method is the difference between the symbols written in the back and in the front ： Pre increment ：`++a`、 Post self increment ：`a++`, Self subtraction is the same .

Next, let's look at why b=1, instead of 2, as a result of ** When post auto increment and post auto decrement participate in other operations , First, the original value is fully involved in other operations , Before self increment ; And the pre self increasing and pre self decreasing , Is to increase or decrease the original value first and then , To participate in other operations .** There is a question of order .

``````let a = 1

let b = a++ //  here  b = 1,a = 2
``````

Because there is a self increasing operation , At the same time, there is an equal operation of assignment , and a The self increment of is postpositive , So the program here will first a Original value 1 Participate in other operations （ Here is the assignment operation ） It will increase itself after , therefore b Received is the original value 1, Then ,a Self increasing to 2. So the result is ：`b=1 a=2`, Self increasing operation is the same .

``````let a = 1
let b = ++a // a = 2 b = 2
``````

According to the above principles , here a The leading self increasing , It will increase itself first , Then participate in the assignment operation , therefore `a=2 b=2`

The assignment operator is a very simple operator , The only thing to be concerned about is the situation of self increase and self decrease .

## Comparison operator

` Greater than (>)、 Less than (<)、 equal (==)、 It's not equal (！=)、 Congruence (===)、 Incongruence (!==)、 Greater than or equal to (>=)、 Less than or equal to (<=)`

The settlement result of the comparison operator , Always a Boolean value , The conditions are `true`, Not established as `false`

``````let a = 2 > 1 	// a = true
let b = -5 > 1 	// b = false
``````

Note the difference between equality and congruence （ Inequality and incompleteness are the same ）

Equality judgment operation , If two operand types are different , Will be converted to the same type first , Then compare , If equal, the result is `true`, conversely `false`, And not congruent operations , If two operand types are different , Return immediately `false`, No type conversion , If the type is the same , Is normal , Return the value according to the result .

``````let a = 1 == "1"	//  Please note that , The first operand is a number 1, The second is the string 1
//  here  a = true, Because this is an equal comparison , So the string 1 Convert to numbers and compare .

let b = 1 === "1" // b = false
//  here , Because the two number types are different , So when congruent operation , Go straight back to false

//  Inequality and incompleteness are the same
``````

When the string is larger , Is to compare their codes by bit .`"3">"20"` obtain `true`

When an object makes an equality judgment , Compare memory addresses .

When comparing different types of values , Refer to the following table for the conversion rules of types ：

value String operating environment Digital computing environment Logical computing environment Object operating environment
undefined “undefined” NaN false Error
null “null” 0 false Error
Non empty string Do not convert The numeric value of a string True
An empty string Do not convert 0 false String
0 “0” Do not convert false Number
NaN “NaN” Do not convert false Number
Infinity “Infinity” Do not convert true Number
Number.POSITIVE_INFINITY “Infinity” Do not convert true Number
Number.NEGATIVE_INFINITY “-Infinity” Do not convert true Number
Number.MAX_VALUE “1.7976931348623157e+308” Do not convert true Number
Number.MIN_VALUE “5e-324” Do not convert true Number
All other numbers “ String value of a number ” Do not convert true Number
true “true” 1 Do not convert Boolean
false “false” 0 Do not convert Boolean
object toString() value() or toString() or NaN true Do not convert
Symbol toString() Error true Symbol
BigInt toString() Do not convert except 0n All are true BigInt

## Logical operators

` And (&&)、 or (||)、 Not (!)`

And operation and or operation can be understood as a pipeline

And means "and" ,`true` Can pass ,`false` Not through

Or just or , `false` adopt ,`true` Not through

It is also called negation .

Logical operators operate on , When calculating , temporary Converts an operand to a Boolean value .

``````let a = 1

let b = 2

let c = a && b //  here  c = 2
``````

And operation ,a Is converted to Boolean `true`, According to the characteristics of the and operator ,`true` Passed , So take the value on the right .c It's equal to the value on the right 2

``````let a = 0

let b = 2

let c = a && b // c = 0
``````

here a Convert to Boolean `false`,`false` Cannot pass `&&`, So it stopped , It's taken a Value , therefore c be equal to 0

Or operation is the opposite of and operation

On the conversion of various values to Boolean values , Just remember the following values ：

JS All values in are as follows 6 A value can be converted to `false`, In addition to this 6 It's worth , Everything else is turned into `true`

1. Numbers ：`NaN`
2. Null pointer ：`null`
3. Undefined ：`undefined`
4. Numbers ：`0`
5. Boolean value ：`false`
6. An empty string ：`“”`

Non operation is the temporary conversion of the value after the operator to a Boolean value , Take the opposite value

``````let a = 1

let b = !a // b = false
``````

a It's the number. 1, To Boolean, the value is `true`, So the opposite value is `false`, be `b = false`

``````let a = 0

let b = !a // b = true
``````
``````let a = ""

let b = !a // b = true
``````
``````let a = "JavaScript It's so simple "

let b = !a // b = false
``````

## 6. Operator priority

Operator priority