portfolio designers contact

Destructuring Assignment in JavaScript

September 29th, 2018

Destructuring assignment (Przypisanie destrukturyzujące, Деструктурирующее присваивание) is a nice feature of modern JavaScript as of ES2015:

var [a, b, …theLot] = [10, 20, 30, 40, 50];
let {age, color} = {color: ‘red’, age: 17}

Then a:number, b:number, theLot:number[], age:number, color: string become available as separate variables in the given scope. It is especially handy when you want to pass a lot of (perhaps, structured) parameters to a function:

function checkOut({age, color=’green’} = {}) {
    console.log(age, color);

You no longer have to care about the order or presence of parameters, and can pass them encapsulated in a single object value!