profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

Destructuring Default Values

Basic syntax

const [x = 3, y] = []
console.log(x, y) // 3 undefined

Computed on demand

Default values are computed when they are needed:

function tralala() {
  return "tralala string"
}

const { computed = tralala() } = {}
console.log(computed) // tralala string

Refer to other variables

Default value an refer to other variables in the same pattern:

const [x = 3, y = x] = [] // x=3; y=3
const [i = 3, j = x] = [7] // i=7; j=7
const [k = 3, l = x] = [7, 2] // k=7; l=2