Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
My mind seems to have been stuck in the era of ES6, and my code seems to be older. Actually, I have come to the world of ES2021, and today I see some new operators.
Comma operator,
I did not know that the comma was an operator.
The code is clear: swap the first and second items of the array and return the sum of the two items
<script>
function sum(arr) {
return ([arr[0], arr[1]] = [arr[1], arr[0]]), arr[0] + arr[1];
}
const list = [1.2];
console.log(sum(list));// Return 3, then list is [2, 1]
</script>
Copy the code
The comma operator evaluates each of its operands (left to right) and returns the value of the last operand.
Numeric separator _
ES2021 introduces the numeric separator _, which provides separation between groups of values, making a long numeric value easier to read. Chrome already provides support for numerical separators, so try it out in your browser.
let number = 100 _0000_0000// The number of zeros is too large to use
console.log(number) / / output 10000000000
Copy the code
In addition, numeric separators can be used for the decimal part of the decimal system, as well as for binary and hexadecimal systems.
0x11_1= = =0x111 // true hexadecimal
0.11 _1= = =0.111 // true is a decimal number
0b11_1= = =0b111 // true binary
Copy the code
The zero merge operator??
?? Is a logical operator that returns the right-hand operand if the left-hand operand is null or undefined, otherwise returns the left-hand operand.
let num1;
let num2 = 2;
num1 ?? num2
Copy the code
let num1 = 3;
let num2 = 2;
num1 ?? num2
Copy the code
The two-bit operator ~~
The advantage of the double negation operator is that it is faster to perform the same operation. You can use the double negation operator instead of math.floor () for positive numbers and math.ceil () for negative numbers.
Math.floor(4.9) = = =4; // true
// Double digit operator ~~~ ~4.9= = =4; // true
Copy the code
Short circuiting operators && and | |
A short-circuit operator is a left-to-right operation in which the former satisfies the requirement and the latter is no longer executed.
&&
For false operation, judging from left to right, if a false value is encountered, the false value is returned, and no further execution, otherwise the last true value is returned||
For true operation, judging from left to right, if a true value is encountered, the true value is returned, no further execution, otherwise the last false value is returned
Ternary expression
Return num2 if num2 === 0 is true, num3 otherwise
const num = num2 === 0 ? num2 : num3;
Copy the code
Short for the assignment operator
-
Addition assigns +=
-
Subtraction assignment -=
-
Multiplication assignment *=
-
Division assignment /=
-
Exponentiate **=
-
Bitwise or copy | =
-
Bitwise and assignment &=
-
Signed bitwise shift right assignment >>=
-
Unsigned bitwise right shift assignment >>>=
-
Logical null assignment?? =