Skip to content

destructuring

In JavaScript, destructuring is syntax that unpacks the elements of an array or the properties of an object into separate variables in a single statement. The pattern on the left of = mirrors the shape of the value on the right.

Array patterns match by position and work on any iterable, such as a Map or a string. Object patterns match by property name. A default value applies only when the matched value is undefined, so null passes through unchanged. Destructuring null or undefined itself throws a TypeError.

JavaScript Destructuring Syntax

Destructuring has an array form and an object form, each with optional defaults and a rest element:

Language: JavaScript Syntax
const [<name1>[ = <default>], <name2>, ...<rest>] = <iterable>;
const { <key>[: <newName>][ = <default>], ...<rest> } = <object>;
[<target1>, <target2>] = <iterable>;
({ <key>: <target> } = <object>);

The first two lines declare new bindings with const, let, or var. The last two assign to existing variables, and an object pattern used this way needs parentheses so the braces don’t parse as a block.

JavaScript Destructuring Examples

Say a function receives an API response and needs the status code, the user’s name from a nested object, and a retry count that the response may omit:

Language: JavaScript
> const response = { status: 200, data: { user: { name: "Ada" } } };
undefined

> const { status, data: { user: { name: userName } }, retries = 3 } = response;
undefined

> [status, userName, retries]
[ 200, 'Ada', 3 ]

The nested pattern reaches into data.user, renames name to userName, and falls back to 3 because response.retries is undefined.

A rest element collects whatever the pattern didn’t name. Here, the first color is the primary one, and the rest go into a new array:

Language: JavaScript
> const [primary, ...others] = ["red", "green", "blue"];
undefined

> others
[ 'green', 'blue' ]

An object rest element works the same way, copying the remaining own enumerable properties into a new object. The ... here collects values, the opposite of spread syntax, which expands them. In a function’s parameter list, the same form is a rest parameter.

Patterns also work in function parameters, where they give each option its own default:

Language: JavaScript
> function connect({ host = "localhost", port = 5432 } = {}) {
…   return `${host}:${port}`;
… }
undefined

> connect({ port: 6543 })
'localhost:6543'

> connect()
'localhost:5432'

The trailing = {} default lets connect() run with no argument, because destructuring undefined would throw.

JavaScript Destructuring Gotchas

  • A default value doesn’t replace null, so const { theme = "light" } = { theme: null } sets theme to null.
  • Destructuring null or undefined throws a TypeError, even with an empty pattern such as const {} = null.
  • A rest element must come last in its pattern, and a trailing comma after it is a SyntaxError.
  • An object rest element makes a shallow copy, so a nested object in it is still shared with the original.

JavaScript Destructuring Use Cases

  • Pulling a few fields out of a JSON response or configuration object
  • Giving function options named parameters with defaults
  • Swapping two variables with [a, b] = [b, a]
  • Looping over Map entries with for (const [key, value] of map)
  • Removing a property from a copy of an object with an object rest element
A developer presses a printed label onto a parcel beside a clay cabinet of keyed drawers marked KEY : VALUE, fed by a JSON order scroll and a SET tag funnel.

Course

From Dicts to JavaScript Objects for Pythonistas

Port a notebook shop's Python shipping label script to JavaScript, and learn how objects, Sets, ?? and ?., and JSON do the everyday work of Python's dicts and sets.

basics js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026