Destructuring Assignment
The concept
Section titled “The concept”The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. Available from ES6 (2015) onward.
OR
Take values from arrays or properties from objects and set them as local variables.
~ Fireship.io
Examples
Section titled “Examples”With arrays
Section titled “With arrays”The basics
Section titled “The basics”// The basicsconst food = ['🥓', '🍕', '🍟', '🍔', '🌮']const [bacon, , , , taco] = food
console.log(bacon, taco) // 🥓 🌮Using the spread operator
Section titled “Using the spread operator”// Using the spread operatorconst food = ['🥓', '🍕', '🍟', '🥬', '🥦']const [, , , ...noJunkFood] = food
console.log(noJunkFood) // [ '🥬', '🥦' ]Using a default value
Section titled “Using a default value”// Using a default valueconst food = [undefined, '🍕', '🍟']const [bacon = '🐖', pizza, fries] = food
console.log(bacon) // 🐖With objects
Section titled “With objects”The basics
Section titled “The basics”// The basicsconst animals = { snake: '🐍', monkey: '🐵', octopus: '🐙',}const { octopus } = animals
console.log(octopus) // 🐙Using the spread operator
Section titled “Using the spread operator”// Using the spread operatorconst animals = { snake: '🐍', monkey: '🐵', octopus: '🐙',}const { ...rest } = animals
console.log(rest) // { snake: '🐍', monkey: '🐵', octopus: '🐙' }
// Overriding a value using the spread operatorconst animals = { snake: '🐍', monkey: '🐵', octopus: '🐙',}const newAnimals = { ...animals, snake: '🦎' }
console.log(newAnimals) // { snake: '🦎', monkey: '🐵', octopus: '🐙' }Using a default value
Section titled “Using a default value”// Using a default valueconst animals = { snake: '🐍', monkey: '🐵', octopus: undefined,}const { octopus = '🦑' } = animals
console.log(octopus) // 🦑Renaming property
Section titled “Renaming property”// Renaming propertyconst animals = { snake: '🐍', monkey: '🐵', octopus: '🦑',}const { octopus: squid } = animals
console.log(squid) // 🦑Nested property
Section titled “Nested property”// Nested propertyconst family = { parent: { child: '👶', },}const { parent: { child },} = family
console.log(child) // 👶Desctructuring within function arguments
Section titled “Desctructuring within function arguments”// Desctructuring within function argumentsconst user = { id: 0, name: 'John',}
function sayHi({ id, name }) { console.log(`Hi ${name}!`)}
sayHi(user) // Hi John!Variables swapping
Section titled “Variables swapping”// Variables swappinglet a = 'foo'let b = 'bar'
;[a, b] = [b, a]
console.log('a:', a, 'b:', b) // a: bar b: fooComputed property name
Section titled “Computed property name”// Computed property nameconst rando = randomKey()
const obj = { [rando]: 42,}
const { [rando]: myKey } = objConditionally added property / value
Section titled “Conditionally added property / value”const trueCondition = trueconst falseCondition = false
const obj = { ...(trueCondition && { '🐕': 'woof' }), ...(falseCondition && { '🐈': 'meow' }),}
console.log(obj)// { '🐕': 'woof' }
const arr = [ ...(trueCondition ? ['🐕'] : []), ...(falseCondition ? ['🐈'] : []),]
console.log(arr)// [ '🐕' ]Resources
Section titled “Resources”Introduction by Fireship.io
Official documentation on MDN
Adoption on CanIUse