wiki/content/20201103113124-destructuring_default_values.md

684 B

date id title
2020-11-03 eb8253f8-f376-4b73-9098-35da64a5a6e7 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