Destructuring

Topic 3 of 5 in Objects6 min readBeginnerFree course

Overview

Destructuring copies values out of an object or array into separate variables. It replaces several lines of repetitive code with one:

JavaScript
const course = { title: 'JavaScript', chapters: 14, level: 'Beginner' };

// Without destructuring
const title = course.title;
const chapters = course.chapters;

// With destructuring
const { title, chapters } = course;
// title = 'JavaScript', chapters = 14

The pattern on the left of = looks like the shape of the data on the right. JavaScript matches the shapes and fills in the variables.

Object destructuring: const { title, chapters: count, price = 0 } = course gives title = ‘JavaScript’ by the same name, count = 14 by renaming, and price = 0 from a default because course has no price. Array destructuring: const [first, , third, …rest] = [10, 20, 30, 40, 50] gives first = 10, skips 20, third = 30 and rest = [40, 50].

Objects

Object destructuring matches by name. The order doesn’t matter, and you can pick just the properties you need:

JavaScript
const { level, title } = course; // order is irrelevant
const { missing } = course;      // undefined — no error

Renaming and defaults

JavaScript
const { title: courseName, price = 0 } = course;
// courseName = 'JavaScript', price = 0 (default, since course has no price)

Read title: courseName as “take title and call it courseName”. You can rename and give a default together: const { price: cost = 0 } = course;.

Defaults are used only when the value is undefined — like default parameters. A property set to null keeps null.

The rest of the properties

...rest collects every property you didn’t name into a new object. It’s a neat way to remove properties without changing the original:

JavaScript
const user = { id: 7, name: 'Asha', password: 'secret' };
const { password, ...safeUser } = user;
// safeUser = { id: 7, name: 'Asha' }

Nested objects

JavaScript
const user = { name: 'Asha', address: { city: 'Pune' } };
const { address: { city } } = user; // city = 'Pune'

Note that this creates city, but not a variable called address — address: here is just the path. Deep nesting quickly becomes hard to read; destructuring one level at a time is often clearer.

Assigning to existing variables

To destructure into variables that already exist, wrap the whole statement in brackets — otherwise the { is read as the start of a block:

JavaScript
let width, height;
({ width, height } = { width: 100, height: 50 });

Arrays

Array destructuring works by position:

JavaScript
const [first, second] = ['Loops', 'Functions', 'Objects'];
const [, , third] = ['Loops', 'Functions', 'Objects']; // skip items
const [head, ...rest] = [1, 2, 3, 4]; // head = 1, rest = [2, 3, 4]
const [x = 0, y = 0] = [5];          // x = 5, y = 0 (default)

It’s especially handy with functions that return arrays or pairs:

JavaScript
const [year, month, day] = '2026-12-03'.split('-');

for (const [key, value] of Object.entries({ a: 1, b: 2 })) {
  console.log(key, value);
}

Swapping values

Destructuring can swap two variables without a temporary one:

JavaScript
let a = 1;
let b = 2;
[a, b] = [b, a]; // a = 2, b = 1

The right side builds a new array [2, 1], and the left side unpacks it back into a and b.

In function parameters

Destructuring works directly in a parameter list — one of its most common uses:

JavaScript
function describe({ title, level = 'Beginner' }) {
  return `${title} (${level})`;
}
describe(course);

The function documents exactly which properties it uses, and callers can pass the object’s properties in any order. It also works in arrow functions and callbacks:

JavaScript
const users = [{ name: 'Asha', age: 30 }, { name: 'Ravi', age: 25 }];
users.map(({ name }) => name);              // ['Asha', 'Ravi']
users.filter(({ age }) => age > 26);        // [{ name: 'Asha', age: 30 }]

const point = ([x, y]) => `(${x}, ${y})`;
point([3, 4]);                              // '(3, 4)'

Destructuring undefined throws. If an argument might be missing, give the whole parameter a default: function describe({ title } = {}).

JavaScript
function connect({ host = 'localhost', port = 80 } = {}) {
  return `${host}:${port}`;
}
connect();               // 'localhost:80' — no error

When to use it

Destructuring is best when you need several values from the same object or array, or when the variable names make the code clearer. For a single property, plain course.title is just as good. And don’t go too deep — a three-level nested pattern is harder to read than two simple lines.

Exercise

Swap two values

Swap the values of a and b using array destructuring — no temporary variable. Then use object destructuring to pull name and city (from address) out of user.

JavaScript
let a = 'first';
let b = 'second';
// swap here

const user = { name: 'Asha', address: { city: 'Pune' } };
// destructure name and city here

Summary

  • Object destructuring picks properties by name; array destructuring by position.
  • You can rename ({ a: newName }), set defaults ({ a = 1 }) and collect the rest (...rest).
  • Defaults apply only when the value is undefined.
  • Destructuring function parameters makes options objects easy to use.
  • Destructuring undefined or null throws — give a default of {} or [].