Continue with the previous article. This article mainly talks about the following operators:
-
Remainder operator (%),
-
Exponential operator (* *),
-
Boolean operator (!),
-
Logical & &
-
Logical or (|)
-
Relational operators (>, <, > =, < =)
1. Remainder operator%
The sign is% and this operator can find the remainder of the division of two operands
// If the operand is a number, the operation is performed normally // If the operand is the value of some other comparison dogs, it will first call Number to convert it into a Number for operation const num = 5; console.log(num % 2); // 1 console.log(num % '2'); // 1 console.log('5' % '2'); // 1 console.log(num % []); // NaN console.log(num % {}); // NaN console.log(num % null);// NaN console.log(num % undefined); // NaN
2. Exponential operator * *
The symbols are * * and math The function pow () has the same function. You can find the n-th power of the number
// If the operand is not a Number or the same routine, call the Number transformation function to convert it into a Number and then perform the operation let num = 3; num = num ** 2 console.log(num); //9 console.log(Math.pow(3, 2)); //9 console.log('3' ** '2'); // 9 console.log('3' ** null); // 1
3. Boolean operator!
The symbol is! Using Boolean operators on a value can obtain the Boolean value corresponding to the value, so as to carry out some logical operations
(1) ! Use for value! Get the result that the Boolean value corresponding to this value is reversed
console.log(!1); // The Boolean value originally corresponding to 1 is true, and false is obtained after negation console.log(!{});// {} the original corresponding Boolean value is true, and false is obtained after negation console.log(!'');// The Boolean value originally corresponding to '' is false, and true is obtained after negation console.log(!'abc');// The Boolean value corresponding to 'abc' is true, and the inverse gets false
(2) !! Use for value!! Get the Boolean value corresponding to this value
console.log(!!1); // The Boolean value originally corresponding to 1 is true console.log(!!{});// The Boolean value originally corresponding to {} is true console.log(!!'');// The Boolean value originally corresponding to '' is false console.log(!!'abc');// The Boolean value corresponding to 'abc' is true
In addition, the number corresponding to true is 1, and the number corresponding to false is 0
Now I have a requirement to convert undefined to 0. What would you write?
console.log(+!!undefined); // 0
4. Logic and operator & &
The symbol is & &. If the left side of the symbol can be values, variables and expressions, the right side can be any logic. If the left side of the symbol is true, the logic on the right side of the symbol will be executed. Otherwise, the logic will directly return the result that the left side is not true Next, let's demonstrate
console.log(1 && 2); // 1 Establishment return 2 console.log({} && null); // null returned for {} establishment console.log(undefined && 3); // Undefined does not hold. The direct logic interrupt returns undefined // Small scenario: when the Boolean value corresponding to my variable (assumed to be flag) is true, I need to send a request // Pseudo code flag && axios.get('****')
5. Logical or operator 𞓜
The symbol is 𞓜. If the left side of the symbol is true, the result of the left side is returned directly. This is a logical interrupt and the code on the right side will not be executed. If the left side of the symbol is not true, the result on the right side is returned
// The result logic established on the left is interrupted, and the code on the right will not be executed console.log({} || num++); // Return to {} console.log(2 || console.log(sum)); // Return 2 console.log(1 || []);// Return 1 // The left side is not tenable console.log(0 || undefined) // Because the Boolean value corresponding to 0 is false, undefined is returned console.log(null || null) // null returns null if null is false console.log(NaN || 3) // The Boolean value corresponding to NaN is false and returns 3 console.log(!!NaN);// Here, verify false
6. Relational operator < > =<=
Comparison rules:
- Both sides are numerical values, which are compared according to the size of the values
- Both sides are strings, which are compared according to the corresponding character code
- Convert a string to a number
- One side is the object. Call the vaueOf method of the object and compare according to the previous rules
- One side is a Boolean value. Convert the Boolean value to a number and compare it
const obj = {} obj.valueOf = 3; console.log(1 < 2); // true console.log('a' < 'b'); // 97 < 98 console.log('a'.charCodeAt(), 'b'.charCodeAt()); // Encoding of output characters console.log('a' < 2); // false console.log(obj < 2); // false 3 < 2 console.log(true < 2); // false 1 < 2
The other next section is about ha