Destructuring — JavaScript

Destructuring takes several items out of an object or an array at once, each into its own variable. const { name, age } = student; takes values out of an…

You receive a student's data as an object, and you only need the name and the age. So far that takes one line per value: one line for student.name, another for student.age. If you need five values, you write five lines that look almost the same.

Destructuring takes all those values out in one line. It is a short way to take several items out of an object or an array at once, and store each item in its own variable. An object is a set of name and value pairs (name: "Budi"). An array is an ordered list of values (["red", "green"]). Think of unpacking a parcel: the parcel is the object or array, the things inside are its items, and the place where you put each thing is a variable.

Objects: take by name

This example takes two values out of the object student:

const student = { name: "Budi", age: 17, city: "London" };

const { name, age } = student;

console.log(name); // Budi
console.log(age); // 17

The result is the same as writing const name = student.name; and then const age = student.age;, but in one line. If a key you name isn't in the object, its variable holds undefined, with no error.

Want a variable name that differs from the key? Use a colon. const { name: studentName } = student; takes the value of the key name and stores it in the variable studentName. It holds "Budi".

Destructuring in a function's parameters

You can also write destructuring right in a function's parameters. The function receives one object, and gets the values it needs as variables right away:

function greet({ name, age }) {
  return "Hello " + name + ", age " + age;
}

console.log(greet({ name: "Budi", age: 17 })); // Hello Budi, age 17

The curly braces in the parameters take name and age out of the object that the call sends. Another way gives the same result: receive the whole object in one parameter, named data for example, then unpack it on the function's first line: const { name, age } = data;.

Arrays: take by position

An array has no keys, so its items are taken by position:

const colors = ["red", "green", "blue"];

const [first, second] = colors;
const [, , third] = colors;

console.log(first);  // red
console.log(second); // green
console.log(third);  // blue

Three dots ... in front of a variable name collect all the remaining items into a new array:

const [one, ...rest] = [1, 2, 3, 4];

console.log(one);  // 1
console.log(rest); // [2, 3, 4]

Swapping two variables

Array destructuring can also swap the values of two variables in one line:

let a = 1;
let b = 2;

[a, b] = [b, a];

console.log(a); // 2
console.log(b); // 1

The line [a, b] = [b, a]; works in two steps:

  1. The right side of = makes a new array [2, 1]: the value of b first, then the value of a.
  2. The left side unpacks that array: the first item goes into a, the second goes into b.

The two variables are made with let, because their values are replaced.

⚠️ A name that doesn't match, a semicolon, and a default that isn't used

🔬 Try it yourself

Unpack other objects and arrays in the tool below, and see which variable gets which value.

TL;DR: Destructuring takes several items out of an object or an array at once, each into its own variable. const { name, age } = student; takes values out of an object by key name (the property name), and a missing key gives undefined. const [first, second] = colors; takes values out of an array by position. A comma with no variable name skips one position, and ...rest collects the remaining items into a new array. You can also destructure in a function's parameters, function greet({ name }). To swap the values of two variables, write [a, b] = [b, a];.

⚡ Objects: take by name

Write:

const student = { name: "Budi", age: 17, city: "London" };

const { name, age } = student;
Guess first: after const { fullName } = student;, what does the variable hold?

undefined, with no error. The object student has a key name, not fullName.

Meaning: This is called destructuring: taking several values out of an object at once. { } on the left of = means "take from an object". The second line makes the variables name and age, holding student.name and student.age. The names inside { } must match the keys (the property names) exactly. Their order doesn't matter, and student itself doesn't change.

⚡ Arrays: take by position

Write:

const colors = ["red", "green", "blue"];

const [first, second] = colors;
const [one, ...rest] = [1, 2, 3, 4];

Meaning: [ ] on the left of = takes items out of an array (an ordered list of values) by position. So you can name the variables anything: first gets the first item, "red". Three dots ... collect the remaining items into a new array, [2, 3, 4].

⚡ Swapping two variables

Write:

let a = 1;
let b = 2;

[a, b] = [b, a];

Meaning: The right side of = makes a new array [2, 1]: the value of b first, then the value of a. The left side unpacks that array: the first item goes into a, the second goes into b. Now a holds 2 and b holds 1.

What you will learn