preface
Hi everyone, I’m Xieyezi, long time no see, I’m back to the Nuggets again, this time for you to sort out 20 ES6 code blocks that are used very frequently, hope you enjoy 😊
The detailed comments and specific usage of the code in this article are posted on my Github, and the source code is linked at the bottom
1. Shuffle the array order
2. Remove all characters except digits
3. Invert strings or words
4. Convert decimal to binary or hexadecimal
5. Merge multiple objects
6. = = =
和 = =
The difference between
7. Deconstruct assignments
8. Swap variable values
9. Determine the palindrome string
10. Optional chain operator
MDN: optional chain operator (? .) allows you to read the value of a property located deep in the chain of connected objects without explicitly validating that each reference in the chain is valid. ? The. Operator functions like the. Chain operator, except that it does not cause errors when references are nullish (null or undefined), and the short-circuit returns undefined. When used with a function call, returns undefined if the given function does not exist
Such as:
if (res && res.data && res.data.success) {
//code
}
Copy the code
Is equivalent to:
if(res? .data? .success) {// code
}
Copy the code
11. The ternary operator
12. Select a value at random from the array
13. Freeze objects
14. Delete duplicate elements from array
15. Reserve the specified decimal place
16. Empty the array
From 17.RGB
convertHEX
18. Get the maximum and minimum values from the array
19. Null value merge operator
MDN: null value 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.
20. The value in the filter array isfalse
The value of the
The source code
20 ES6 tips you can’t miss
Above, code word drawing is very hard, but also hope not stingy hands of praise, your praise is I continue to update the biggest power 😊!