1. Assign an array item to a variable using object destruct:

const str = “1997,kangkang,boy,23” 

const {1:name,2:sex,0:age} = str.split(‘,’) 

console.log(name,sex,age) //kangkang boy 1997

Create a 100% pure Object that inherits none of Object’s properties and methods (e.g. **constructor**, **toString()**, etc.) :

const obj = Object.create(null) console.log(obj)//{}

3. Sum all the values in the array

var numbers = [3, 5, 7, 2]; var sum = numbers.reduce((x, y) => x + y,0); 

console.log(sum); // returns 17

4. Comma operator

let x = 1; x = (x++, x); console.log(x); // expected

 output: 2 x = (2, 3); console.log(x); // expected output: 3

5. Easily remove duplicates from arrays:

const removeDuplicate = arr=>[…new Set(arr)]

Let result = removeDuplicate([42,42,’11’,’11’,true,true,null,null]) console.log(result)//[42, “11”, true,null]

6. Use the extension operator to quickly flatten a two-dimensional array:

function flattenArray(arr){ 

const flatArr = [].concat(… arr)

return flatArr.some(item=>Array.isArray(item))? flattenArray(flatArr):flatArr

 } 

Const arr = [1, 2, 3], [4, 5, [6, 7, [8, 9]]]]

 console.log(flattenArray(arr))//[1, 2, 3, 4, 5, 6, 7, 8, 9]

7. Objects are converted to arrays

Const obj = {} in 1-0, 1:1, 2:2, tie

const menbers = Array.from(obj )

The console. The log (menbers) / /,1,2,3 [1]

7 Obtain the maximum or minimum value in the array

Const arr = [6]

const max = Math.max(… arr)

console.log(max) // 6

8 Check whether the two arrays are the same

export function scalarArrayEquals(array1, array2) { 

 return array1.length === array2.length && array1.every(function(v, i) { 

return v ===array2[i]})

 }

9javaScript null value merge operator (??)

The number on the right is returned only if the left side is null and undefined

  • 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.

  • With the logical or operator (| |), logical or operator will be on the left operand is false value returns the right operand. That is to say, if you use the | | to set default values for certain variables, may encounter unexpected behavior. For example, false (for example, ” or 0).

10. Optional javaScript chain operators (? .).

The 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 nullish does not cause an error and the expression shorts out the return value

Use the optional chain operator (? .) The browser does not display errors!

**let result = someOne. CustomMethod? . ();

** Optional chain with expression: **let nestedProp = obj? .[‘prop’ + ‘Name’];

** Let arrayItem = arr? . [42];