Arithmetic operator
Y = 5
“+” as the binary operator
The rules
- Infinity + Infinity = Infinity
- (-Infinity) + (-Infinity) = -Infinity
- Infinity + (-Infinity) = NaN
- Infinity + null = Infinity
- 0 plus 0 is 0
- Minus 0 plus minus 0 is minus 0
- Plus 0 plus minus 0 is plus 0
- If there are two strings, the strings are concatenated
- If one of them is a string and one of them is not, the non-string is converted to a string and then concatenated
- 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
- Infinity – Infinity = NaN
- (-Infinity) – (-Infinity) = -NaN
- Infinity – (-Infinity) = Infinity
- Infinity – null = Infinity
- 0 minus 0 is 0
- -0 – -0 = -0
- Plus 0 minus minus 0 is plus 0
- The subtraction operations associated with NaN all result in NaN
- If one of the operands is an object, the object is implicitly converted and subtracted according to the previous rules
- 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
- Infinity * 0 = NaN
- Infinity * null = NaN
- Infinity * undefined = NaN
- Infinity * Infinity = Infinity
- If the value of the operand exceeds the representation range of the value, the result is Infinity or -infinity
- If one of the operands is NaN, the result is NaN
- If one of the operands is undefined, the result is NaN
- If one of the operands is an object, the object is implicitly converted and then multiplied according to the previous rules
- 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
- Infinity / 0 = Infinity
- Infinity / null = Infinity
- Infinity / undefined = NaN
- Infinity / Infinity = NaN
- If the value of the operand exceeds the representation range of the value, the result is Infinity or -infinity
- If one of the operands is NaN, the result is NaN
- If one of the operands is undefined, the result is NaN
- If one of the operands is an object, the object is implicitly converted and then divided according to the previous rules
- 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
- The operands are numeric and perform the usual division calculation, returning the remainder of the division
- Any number % undefined = NaN
- Any number % NaN = NaN
- Infinity % Any number = NaN
- Finitely large number % 0 = NaN
- Finitely large number % Infinity = finitely large number
- 0 % except null, undefined, NaN any number = 0
- If one of the operands is an object, the object is implicitly converted and then the mod is performed according to the previous rules
- If one of the operands is not a Number, the Number() conversion is called
“+ +” operator
The rules
- When applied to a non-numeric type, it is converted to a number before performing the ++ operation
- Applied to floating point numbers, incrementing by one
- Applied to objects, the object is implicitly converted, followed by ++ operations according to the previous rules
“-” operator
The rules
- When applied to a non-numeric type, it is converted to a number before performing the — operation.
- Applied to floating point numbers, performing the operation of minus one
- 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
- If the operand is Boolean, it is converted to a value before comparison, false to 0 and true to 1
- If the operand is a character and the other is an object, the object is implicitly converted and then compared
- If the operand is of character type and the other operand is a value, then the string is converted to a value in comparison
- Null == undefined is true
- Do not want to wait for anything other than null and undefined.
- NaN is not equal to any number
- If both operands are objects, compare whether they are the same object, and return true if so, false otherwise
Relational operator
>, <, >=, <=
The rules
- If both operands are numeric, a numeric comparison is performed
- If both operands are strings, compare the ASCII codes corresponding to the two strings
- If an operand is an object, the object is implicitly converted and then compared.
- 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
- If the first operand is an object, return the second operand
- If the second operand is an object, the object is returned only if the value of the first operand is true
- If both operands are objects, return the second operand
- If one of the operands is NULL, null is returned
- NaN is returned if one of the operands is NaN
- Returns undefined if one of the operands is undefined
“| |” operator
The rules
- Returns the first operand if the first operand is an object
- The second operand is returned if the first operand evaluates to false
- Return the first operand if both operands are objects
- If both operands are NULL, null is returned
- NaN is returned if both operands are NaN
- If both operands are undefined, undefined is returned
“!” The operator
The rules
- Returns true if the operand is an empty string
- Returns true if the operand is the value 0
- Returns true if the operand is null
- Return true if the operand is NaN
- Returns true if the operand is undefined
- 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