Destructuring
Overview
Destructuring copies values out of an object or array into separate variables. It replaces several lines of repetitive code with one:
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 = 14The pattern on the left of = looks like the shape of the data on the right. JavaScript matches the shapes and fills in the variables.
Objects
Object destructuring matches by name. The order doesn’t matter, and you can pick just the properties you need:
const { level, title } = course; // order is irrelevant
const { missing } = course; // undefined — no errorRenaming and defaults
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:
const user = { id: 7, name: 'Asha', password: 'secret' };
const { password, ...safeUser } = user;
// safeUser = { id: 7, name: 'Asha' }Nested objects
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:
let width, height;
({ width, height } = { width: 100, height: 50 });Arrays
Array destructuring works by position:
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:
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:
let a = 1;
let b = 2;
[a, b] = [b, a]; // a = 2, b = 1The 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:
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:
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
undefinedthrows. If an argument might be missing, give the whole parameter a default:function describe({ title } = {}).
function connect({ host = 'localhost', port = 80 } = {}) {
return `${host}:${port}`;
}
connect(); // 'localhost:80' — no errorWhen 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.
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.
let a = 'first';
let b = 'second';
// swap here
const user = { name: 'Asha', address: { city: 'Pune' } };
// destructure name and city herelet a = 'first';
let b = 'second';
[a, b] = [b, a];
const user = { name: 'Asha', address: { city: 'Pune' } };
const { name, address: { city } } = user;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
undefinedornullthrows — give a default of{}or[].