Skip to main content

Why destructuring?

Destructuring lets you unpack values from objects and arrays into separate variables in a single line. Instead of writing user.name, user.email, user.role over and over, you extract them once.

Object destructuring

Pull specific properties out of an object by name:
Python mental model: object destructuring is like doing multiple dict lookups and assignments in one step (name = user["name"], email = user["email"]) — but with a much shorter syntax.

Renaming variables

When the property name conflicts with an existing variable or you want a clearer name:

Default values

Set fallbacks for properties that might not exist:
Python mental model: this is similar to settings.get("language", "en"). One key difference: destructuring defaults only apply when the property is undefined (missing). If the value is explicitly null, the default is not used.

Nested destructuring

Don’t go more than two levels deep with nested destructuring. It quickly becomes unreadable. For deeply nested data, destructure in multiple steps or use dot notation.

Array destructuring

Pull values out by position:
Python mental model: this is like tuple unpacking (first, second = values) — but JavaScript adds useful extras like skipping items and collecting “the rest” with ...remaining.

Skipping items

Swapping variables

Where you’ll use destructuring every day

React’s useState

You’ve already seen this — it’s array destructuring:
useState returns a two-element array: [currentValue, setterFunction]. Destructuring gives them meaningful names.

React component props

Function parameters

If you’re coming from Python, this feels like writing def display_order(order): ... and immediately doing id = order["id"], customer = order["customer"], total = order["total"] at the top of the function.

API responses

Comparing to Python

Object destructuring is one of the biggest syntax wins JavaScript has over Python. It’s everywhere in React code.

What’s next?

Destructuring unpacks values out of objects and arrays. The spread operator does the opposite — it expands them into new structures.

Spread operator

Copy, merge, and expand arrays and objects