??

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

1 / / examples

let name = null

let test = name ?? 'hahaha'

console.log(test) // hahaha

2 / / examples

let name = null

let test = name ?? 'hahaha'

console.log(test) // hahaha

3 / / examples

let name = '123'

let test = name ?? 'hahaha'

console.log(test) / / 123

Copy the code

? .

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


1 / / examples

let test = {
  person: {name:"xiaoming"}}letxm = test.data ? .nameconsole.log(xm) // undefined

2 / / examples

let test = {
  person: {name:"xiaoming"}}letxm = test.person ? .nameconsole.log(xm) // xiaoming

Copy the code

globalThis

GlobalThis: is an environment global attribute of this. detailed


console.log(globalThis)

Copy the code

Browser environment results

Node environment results

Promise.allSettled()

Promise.allsettled (): Returns a Promise after all the given promises have fulfilled or rejected, with an array of objects, each representing the corresponding Promise result. detailed


1 / / examples

let p1 = Promise.reject(1)

let p2 = Promise.resolve(2)

Promise.allSettled([p1,p2]).then(data= >{
  console.log(data) // [{status: "rejected", reason: 1},{status: "fulfilled", value: 2}]
})

2 / / examples

let p1 = Promise.reject(1)

let p2 = Promise.reject(2)

Promise.allSettled([p1,p2]).then(data= >{
  console.log(data) // [{status: "rejected", reason: 1},{status: "rejected", reason: 2}]
})

3 / / examples

let p1 = Promise.resolve(1)

let p2 = Promise.resolve(2)

Promise.allSettled([p1,p2]).then(data= >{
  console.log(data) // [{status: "fulfilled", value: 1},{status: "fulfilled", value: 2}]
})

Copy the code

import()

Import (): Dynamic import of JS modules. detailed


1 / / examples
function test(){
  import('./xxxx').then(res= >console.log(res))
}

2 / / examples
function test(){
 let res = await import('./xxxx')
 console.log(res)
}
Copy the code

String.prototype.matchAll

String. The prototype. MatchAll: returns a contains all the results of the regular expression matching and the packet capture group of iterators. detailed


const regexp = /t(e)(st)? /g

const str = 'test1te'

const array = [...str.matchAll(regexp)]

console.log(array) // [["test", "e", "st", index: 0, input: "test1te", groups: undefined],["te", "e", undefined, index: 5, input: "test1te", groups: undefined]]
[result,$1,$2,$3...]
Copy the code