Arithmetic operator

Y = 5

“+” as the binary operator

The rules

  1. Infinity + Infinity = Infinity
  2. (-Infinity) + (-Infinity) = -Infinity
  3. Infinity + (-Infinity) = NaN
  4. Infinity + null = Infinity
  5. 0 plus 0 is 0
  6. Minus 0 plus minus 0 is minus 0
  7. Plus 0 plus minus 0 is plus 0
  8. If there are two strings, the strings are concatenated
  9. If one of them is a string and one of them is not, the non-string is converted to a string and then concatenated
  10. If none of the operands are strings, convert the operands to numeric values and add them.

“+” as unary operator

role

Converts an object of another type to a Number type as in Number()

    let two = "2";
    let three = "3";
    console.log(+two + +three); / / 5
Strings are converted to numbers before being added, because unary operators have higher precedence than binary operatorsCopy the code

“-” operator

The rules

  1. Infinity – Infinity = NaN
  2. (-Infinity) – (-Infinity) = -NaN
  3. Infinity – (-Infinity) = Infinity
  4. Infinity – null = Infinity
  5. 0 minus 0 is 0
  6. -0 – -0 = -0
  7. Plus 0 minus minus 0 is plus 0
  8. The subtraction operations associated with NaN all result in NaN
  9. If one of the operands is an object, the object is implicitly converted and subtracted according to the previous rules
  10. If one of the operands is non-numeric, the Number() function is called in the background to convert it to a value, and then the subtraction is performed according to the previous rules.

For those unfamiliar with implicit conversions, check out this article to get a feel for Javascript implicit conversions

“*” operator

The rules

  1. Infinity * 0 = NaN
  2. Infinity * null = NaN
  3. Infinity * undefined = NaN
  4. Infinity * Infinity = Infinity
  5. If the value of the operand exceeds the representation range of the value, the result is Infinity or -infinity
  6. If one of the operands is NaN, the result is NaN
  7. If one of the operands is undefined, the result is NaN
  8. If one of the operands is an object, the object is implicitly converted and then multiplied according to the previous rules
  9. If one of the operators is not of a numeric type, the Number() is converted and the multiplication is performed according to the previous rules

“/” operator

The rules

  1. Infinity / 0 = Infinity
  2. Infinity / null = Infinity
  3. Infinity / undefined = NaN
  4. Infinity / Infinity = NaN
  5. If the value of the operand exceeds the representation range of the value, the result is Infinity or -infinity
  6. If one of the operands is NaN, the result is NaN
  7. If one of the operands is undefined, the result is NaN
  8. If one of the operands is an object, the object is implicitly converted and then divided according to the previous rules
  9. If one of the operators is not of a numeric type, the conversion is performed by calling Number() and then dividing according to the previous rules

“%” operator

The rules

  1. The operands are numeric and perform the usual division calculation, returning the remainder of the division
  2. Any number % undefined = NaN
  3. Any number % NaN = NaN
  4. Infinity % Any number = NaN
  5. Finitely large number % 0 = NaN
  6. Finitely large number % Infinity = finitely large number
  7. 0 % except null, undefined, NaN any number = 0
  8. If one of the operands is an object, the object is implicitly converted and then the mod is performed according to the previous rules
  9. If one of the operands is not a Number, the Number() conversion is called

“+ +” operator

The rules

  1. When applied to a non-numeric type, it is converted to a number before performing the ++ operation
  2. Applied to floating point numbers, incrementing by one
  3. Applied to objects, the object is implicitly converted, followed by ++ operations according to the previous rules

“-” operator

The rules

  1. When applied to a non-numeric type, it is converted to a number before performing the — operation.
  2. Applied to floating point numbers, performing the operation of minus one
  3. Applied to objects, the object is implicitly converted, followed by — operations according to the previous rules

“* *” operator

role

Exponentiation

grammar

Operands ** powers

    console.log(2六四屠杀3);     / / 8
    console.log(8* * (1/3)); / / 2
Copy the code

The assignment operator

=, +=, -=, *=, /=, %=

So x is equal to 10, y is equal to 5


Comparison operator

==, ==,! =,! = =

X = 5


The rules

  1. If the operand is Boolean, it is converted to a value before comparison, false to 0 and true to 1
  2. If the operand is a character and the other is an object, the object is implicitly converted and then compared
  3. If the operand is of character type and the other operand is a value, then the string is converted to a value in comparison
  4. Null == undefined is true
  5. Do not want to wait for anything other than null and undefined.
  6. NaN is not equal to any number
  7. If both operands are objects, compare whether they are the same object, and return true if so, false otherwise

Relational operator

>, <, >=, <=

The rules

  1. If both operands are numeric, a numeric comparison is performed
  2. If both operands are strings, compare the ASCII codes corresponding to the two strings
  3. If an operand is an object, the object is implicitly converted and then compared.
  4. If an operand is of a non-numeric type, it is converted to a numeric type for comparison

Logical operator

So x is equal to 6, y is equal to 3


“&” operator

The rules

  1. If the first operand is an object, return the second operand
  2. If the second operand is an object, the object is returned only if the value of the first operand is true
  3. If both operands are objects, return the second operand
  4. If one of the operands is NULL, null is returned
  5. NaN is returned if one of the operands is NaN
  6. Returns undefined if one of the operands is undefined

“| |” operator

The rules

  1. Returns the first operand if the first operand is an object
  2. The second operand is returned if the first operand evaluates to false
  3. Return the first operand if both operands are objects
  4. If both operands are NULL, null is returned
  5. NaN is returned if both operands are NaN
  6. If both operands are undefined, undefined is returned

“!” The operator

The rules

  1. Returns true if the operand is an empty string
  2. Returns true if the operand is the value 0
  3. Returns true if the operand is null
  4. Return true if the operand is NaN
  5. Returns true if the operand is undefined
  6. Otherwise return false

An operator


The “&” bitwise and operator

role

Amand is performed on each of the corresponding bits of the binary operand, yielding 1 if both bits are 1 and 0 if one of the corresponding bits is 0

1The binary representation of is:00000000 00000000 00000000 00000001
3The binary representation of is:00000000 00000000 00000000 00000011 
console.log(1 & 3) / / 1
Copy the code

“|” the bitwise or operator

role

| in the operands of a binary number corresponding to each do or operation, if the corresponding bit is 0 the result is 0 and if the corresponding bit is 1, the result is 1

1The binary representation of is:00000000 00000000 00000000 00000001
3The binary representation of is:00000000 00000000 00000000 00000011
console.log(1 | 3) / / 3
Copy the code

The “~” bitwise non-operator

role

~ does a nonoperation on each of the binary bits of the operand, yielding 1 if the corresponding bit is 0 and 0 if the corresponding bit is 1

1The binary representation of is:00000000 00000000 00000000 00000001 
3The binary representation of is:00000000 00000000 00000000 00000011 
----------------------------- 
1Inverse binary representation:11111111 11111111 11111111 11111110 
Because the first digit (sign bit) is1So this number is a negative number. JavaScript internally uses the form of complement to represent negative numbers, which requires subtracting them1Take the negative one more time, and then add the negative sign to get the corresponding negative number10Hexadecimal values.----------------------------- 1Radix-minus-one complement reduce1:11111111 11111111 11111111 11111101 Then take the:00000000 00000000 00000000 00000010 Represented as a10Base plus minus sign:2 - console.log(~ 1) / / - 2 Copy the code

“^” bitwise xor operator

role

^ Does xor to each of the corresponding binary bits of the operand, resulting in 1 if the corresponding bits are different, and 0 otherwise

1The binary representation of is:00000000 00000000 00000000 00000001
3The binary representation of is:00000000 00000000 00000000 00000011 /
console.log(1 ^ 3) / / 2
Copy the code

The “<<” left-shift operator

role

The << operator shifts all bits of the specified value’s binary number to the left by the specified number of digits

Mobile rules

Discard the high position and fill the low position with 0, that is, move all the digits to the left according to the binary form, move the high position out (discard), and fill the low position with 0.

1The binary representation of is:00000000 00000000 00000000 00000001 
2The binary representation of is:00000000 00000000 00000000 00000010 
console.log(1 << 1) / / 2
Copy the code

“>>” has a sign shift right operator

role

The >> operator shifts all bits of the binary number of the specified value to the right by the specified number of digits

Mobile rules

Bits moved to the right are discarded and the leftmost bits are copied to fill the left side. Since the new leftmost bit is always the same as before, the sign bit is not changed.

1The binary representation of is:00000000 00000000 00000000 00000001 
0The binary representation of is:00000000 00000000 00000000 00000000 
console.log(1 >> 1) / / 0
Copy the code

“>>>” Unsigned right-shift operator

role

The >>> operator shifts all bits of the binary number of the specified value to the right by the specified number of digits

Mobile rules

Bits moved to the right are discarded and the left side is filled with zeros. Because the sign bit becomes zero, the result is always non-negative. For non-negative numbers, signed and unsigned right shifts always return the same result.

Conditional operator

grammar

Judgment condition? Operation when condition is true: Operation when condition is false

let num = 2;
num > 1 ? console.log("To") :console.log("Wrong"); / / "to"
Copy the code

Type operator

The typeof operator

role

Returns the type of the variable

    console.log(typeof null);     // object 
    console.log(typeof undefined);// undefined
    console.log(typeof []);       // object
    console.log(typeof {});       // object
    console.log(typeof true);     // boolean
 console.log(typeof "123"); // string  console.log(typeof 123); // number Copy the code

Typeof detection type is not accurate. For the introduction and comparison of accurate detection methods and various detection methods, please see JS data type detection in depth

instanceof

grammar

a instance of

role

Check if a is an instance of B, true if it is, false if it is not

Matters needing attention

The prototype chain can be modified, so this method is also inaccurate, and the details are viewed in depth to understand THE JS data type detection

Other operators

The “.” dot operator

role

Read the property value of the object, the point can only be used on the object, if the point is not before the object, it will be changed to an object, after the call and then changed back to the original type

Void operator.

role

Evaluate the expression, or execute the statement, and void always has undefined.

Return false in the a tag prevents the default action; void(f()) does the same.

“,” comma operator

grammar

Expression 1, expression 2… , expression n

role

Ignore the first operand and return the second

Matters needing attention

The comma operator has a lower precedence than “=”

The delete operation

role

The delete operator is used to delete an object property or array element, and returns true if the deletion succeeds or if the deleted target does not exist.

Matters needing attention

Not all attributes can be deleted. Some built-in core and client attributes cannot be deleted. Variables declared by the var statement cannot be deleted, nor can functions defined by the function statement.

The in operator.

grammar

a in b

role

Checks whether operand A exists in object B as an attribute name, true if it does, false if it does not

    let obj = {
        a: 1
    }
    console.log("a" in obj); // true
Copy the code