Destructuring Assignment

Destructuring assignments allow to extract data from arrays and objects into individual variables. It is a shorthand for creating a new variable for each value of an array or object.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Destructuring an object
const object = {
age: 35,
firstName: 'Benny'
};

const {age, firstName} = object;

console.log(`${firstName} is ${age} years old.`);

// Destructuring an array
const array = [300, 500];

const [min, max] = array;

console.log(`The range is between ${min} and ${max}.`);