ES6+ Modern JavaScript

Destructuring

Destructuring lets you unpack values from arrays and objects into named variables in a single, readable expression. It eliminates repetitive property access code and is one of the most widely-used ES6 features.

What is destructuring in JavaScript?

Destructuring unpacks values from arrays or properties from objects into variables in one line. const { name, age } = user; creates name and age from the object, and const [first, second] = list; takes the first two array items. You can rename, set defaults and destructure function parameters directly.

const { name, role = "student", address: { city } } = user;
const { name: userName } = user;          // rename
const [first, , third, ...rest] = [1, 2, 3, 4, 5];
[a, b] = [b, a];                          // swap

function show({ title, done = false }) { … }   // parameter destructuring

Array Destructuring

// Basic — position-based
const [a, b] = [1, 2];
// Skip with comma
const [, second] = [1, 2];
// Rest
const [head, ...rest] = [1,2,3];
// Default value
const [x = 0, y = 0] = [5];
// Swap
[a, b] = [b, a];

Object Destructuring

// Basic — name-based
const { name, age } = user;
// Rename alias
const { name: n } = user;
// Default value
const { score = 0 } = user;
// Nested
const { addr: { city } } = user;
// Rest
const { name, ...rest } = user;

Destructuring in Function Parameters

Instead of accessing options.timeout inside the function, destructure right in the parameter list. This makes the expected shape of the argument explicit and provides defaults.

Without destructuring

function connect(options) {
const host = options.host;
const port = options.port || 3000;
...
}

With destructuring

function connect({ host, port = 3000 }) {
// host and port ready to use
...
}

Tip: When destructuring in function parameters, provide a default for the whole parameter to handle the case when it is called with no argument: function f({ x = 0 } = {}) {}

Common Pitfalls

Object destructure without braces: const name = user; (assignment, not destructure)
Destructure with braces: const { name } = user;
Destructuring null or undefined crashes with TypeError
Provide a default: const { name } = user ?? {}; or guard with if (user)
Deeply nested destructuring making code hard to read
Limit nesting to 2 levels. Destructure in stages for clarity.
Forgetting that defaults only apply when value is undefined (not null, 0, or false)
Explicit null check: const { val = 0 } = { val: null }; → val is still null

Interactive Examples

Code Editor

<pre id="out"></pre>

Preview

Destructuring — step by step

1 / 4
1

Array destructuring — position-based

const [a, b, c] = [1, 2, 3] unpacks by position. You can skip elements with empty commas: const [first, , third] = arr. Use rest syntax to capture the remainder: const [head, ...tail] = arr. Default values fill in when the value is undefined: const [x = 0] = [].

1 of 4
💡Destructuring with API responses
When consuming REST APIs, destructure response data immediately to eliminate deeply chained property access. Pull out data, error, and status at the top of your handler — this makes the expected shape of the response explicit at the point of consumption and keeps the rest of the function clean.

🎯 Quick Check

What does const { x = 10 } = {} produce for x?

JavaScript Destructuring: summary

Key points

  • Defaults apply only when the value is undefined, not null.
  • Destructuring a property of undefined throws; give nested objects a default ({ address = {} }).
  • Rest (...rest) collects the remaining items or properties.

Frequently asked questions

›How do I destructure with a default value?

Add = value after the name: const { theme = "light" } = settings;.