Operator and implicit conversion rules

Logic not, logic with, logic or

  • Boolean operators includeThe logical not!.Logic and &&.Logic or | |.
  • The logical not!No matter what type is applied, it always returns a Boolean value.
The operand The return value
object false
An empty string true
Non-empty string false
The number 0 true
Non-zero values (including infinity) false
null true
NaN true
undefined true
  • If you are using!!!!!Theta is the same as thetaBoolean()Method, the first exclamation mark converts the operand to a Boolean value, and the second non takes the reverse to get the true Boolean value of the variable.
  • Logic and &&A false or false
  • Logic or | |A true is true
  • Logic or | |If the first operand is true then we don’t evaluate it, we can use it in real life||To avoid assigning values to variablesnullorundifined

Multiplicative operator

  • The special rules for multiplicative operators are as follows:
The operand 1 symbol The operand 2 The rules
Is the numerical * Is the numerical Get a positive value and print infinity out of range
A negative value * A negative value Get a positive value and print infinity out of range
Is the numerical * A negative value Get negative value, out of range -infinity
A negative value * Is the numerical Get negative value, out of range -infinity
NaN * Arbitrary operand Output NaN
infinty * 0 Output NaN
infinty * Non-zero finite value Output infinty or -infinty according to the second operand symbol
infinty * infinty Output infinty

If it is not a numeric operand, it can be converted using the Number() method and then evaluated

The division operator

  • The special rules for the division operator are as follows:
The operand 1 symbol The operand 2 The rules
Is the numerical / Is the numerical Get a positive value and print infinity out of range
A negative value / A negative value Get a positive value and print infinity out of range
Is the numerical / A negative value Get negative value, out of range -infinity
A negative value / Is the numerical Get negative value, out of range -infinity
NaN / Arbitrary operand Output NaN
infinty / infinty Output NaN
0 / 0 Output NaN
Non-zero finite value / 0 Prints infinty or -infinty based on the first operand symbol
infinty / Any numerical Prints infinty or -infinty according to the second operand symbol

If it is not a numeric operand, it can be converted using the Number() method and then evaluated

The modulo operator

  • The special rules for the modulo operators are as follows:
The operand 1 symbol The operand 2 The rules
The numerical % The numerical Follow the division rule and print the remainder
Infinite value % Limited value Output NaN
Limited value % 0 Output NaN
infinty % infinty Output NaN
Limited value % Infinite value Output finite value
0 % Non-zero value 0

If it is not a numeric operand, it can be converted using the Number() method and then evaluated

Exponential operator

  • The exponent operator is**, the calculation method is similar toMath.pow()
Console. log(3 ** 2) // outputs 9 console.log(math.pow (3,2)) // outputs 9Copy the code
  • The index assignment operator is* * =
let squard = 3; squard **= 2; Console. log(squard) // Prints 9Copy the code

The additive operator

  • The additive operator is usually used to find the sum of two numbers
  • The rule for the additive operator for two values:
The operand 1 symbol The operand 2 The rules
NaN + Any numerical Output NaN
infinty + infinty Output infinty
-infinty + -infinty Output – infinty
infinty + -infinty Output NaN
+ 0 + + 0 Output + 0
+ 0 + 0 Output + 0
0 + 0 Output – 0
  • Rules for the additive operator on strings:
The operand 1 symbol The operand 2 The rules
A string of 1 + String 2 Concatenated string
A string of 1 + The string The second operand is converted to a string and then concatenated

The subtraction operator

  • The subtraction operator rules for two values:
The operand 1 symbol The operand 2 The rules
Value of 1 - 2 Perform mathematical subtraction and return the result
NaN - The numerical Returns NaN
infinty - infinty Returns NaN
-infinty - -infinty Returns NaN
infinty - -infinty Return infinty
-infinty - infinty Return – infinty
+ 0 - + 0 Return + 0
+ 0 - 0 Return 0
0 - 0 Return + 0
String, Boolean, NULL, undipay - The numerical The non-numeric call Number() method is first converted and then subtracted
object - The numerical ValueOf () is called first, and toString() is not called by valueOf before subtraction

Relational operator

  • The relational operators includeLess than (<).Greater than (>).Less than or equal to.Greater than or equal to (>=Each of these operations returns a Boolean value.
  • Rules for relational operators:
The operand 1 symbol The operand 2 The rules
Value of 1 <.>.< =.> = 2 Perform numerical comparison
A string of 1 <.>.< =.> = String 2 Compares the encoding of the corresponding character in the string one by one
The numerical <.>.< =.> = Arbitrary operand Converts non-numeric operands to numeric values for numerical comparison
object <.>.< =.> = Arbitrary operand ValueOf () is called first, and toString() is not called by valueOf
Boolean value <.>.< =.> = Arbitrary operand It converts them into numbers and then compares them

Equality operator

  • Equal to (= =) and do not equal (! =), both operators advance the cast to determine whether the operands are equal.
  • Equality operator conversion rules:
The operand 1 symbol The operand 2 The rules
Boolean value = =or! = The numerical Boolean values are converted to values for comparison, true to 1 and false to 0
string = =or! = The numerical Attempts are made to convert strings to numeric values for comparison
object = =or! = The numerical The valueOf() method is tried to get the original valueOf the object and compare it
NaN = =or! = Arbitrary operand == returns false,! = return true
object = =or! = object Compares whether two operands refer to the same object
null = =or! = undifined Null and Undiam are equal, and they cannot be translated into other types for comparison
  • Congruent (= = =) and incongruence (! = = =) must be equal without casting, otherwise false. Here is a special case, NULL == UNdipay (return true), null === Undipay (return false).

Conditional operator

  • The conditional operator is also known as the ternary expression operator, i.eConditions? Result 1: Result 2, where the condition true returns 1, and false returns 2.

The assignment operator

  • For simple assignment operations=You can do it
  • The compound assignment operators are shown below, and there is no performance benefit to using these operators.
expression symbol The rules
a *= b Multiply and assign (* =) a = a * b
a /= b Assignment after division (/ =) a = a / b
a %= b Modulo assignment (% =) a = a % b
a += b Append assignment (+ =) a = a + b
a -= b Subtraction assignment (- =) a = a – b

There are also some other bit operators and compound operators, in the actual development of the use of little, interested in their own information.

Comma operator

  • The comma operator is used to perform multiple operations in a single statement. The most common situation is when colleagues declare multiple variables

let num1 = 1 , num2 = 2 , num3 = 3;