JavaScript ECMAScript 2024 ·
✓ verified by execution on 2026-08-14
When working with large objects, writing user.profile.email or user.id repeatedly gets tedious. JavaScript provides a powerful shorthand called destructuring assignment to cleanly unpack properties from objects directly into distinct variables.
Instead of declaring variables line by line, you can extract multiple properties at once using curly braces {}.
A common misconception is that the variables in your destructuring block must be listed in the exact same order as the properties in the object. This is false!
While array destructuring relies on position, object destructuring strictly matches by the property name. The order you list them inside the curly braces is completely irrelevant.
javascript
const user = { id: 42, username: 'jdoe' };const { username, id } = user;console.log(username);console.log(id);
Output
jdoe
42
Your output
Visualizing the Memory Map
Let’s watch exactly how the JavaScript engine maps the properties from the object heap into your local execution context.
Destructuring by key: const { theme } looks up the property named theme on the object in the heap and binds a local variable of the same name to its value.
Renaming Variables
Sometimes the property name in the object conflicts with a variable you already have, or it just isn’t descriptive enough. You can assign an unpacked property to a completely new variable name using a colon :.
Think of it as reading "unpack the name property, and assign it to fullName".
If you attempt to destructure a property that doesn’t exist, JavaScript won’t crash—it will just assign undefined to your variable. To prevent this, you can safely provide fallback default values using the = operator.
javascript
const user = { name: 'Alice' };const { role = 'guest' } = user;console.log(role);
Output
guest
Your output
Nested Destructuring
Objects are frequently nested. You can mirror the exact structure of the object in your destructuring assignment to reach deep inside and extract specific values in a single line.
While destructuring is incredibly convenient, there is one fatal trap you must watch out for. What do you think happens if you try to destructure an object that is currently null or undefined?
Predict the outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
const user = null;try { const { id } = user;} catch (e) { console.log(e.name);}
Output
TypeError
You predicted
Attempting to destructure null or undefined throws a TypeError immediately. This happens before any default values are even considered! Always ensure the parent object actually exists before attempting to destructure it.
Check yourself
Why does object destructuring ignore the order of variables inside the curly braces?
Reveal answer
It matches variables directly by their property key name in the object. — Unlike arrays, which are ordered, objects use key-value pairs. Destructuring looks for a matching key name to pull out the corresponding value.
What happens if you try to destructure a property that does not exist on the object (and provide no default value)?
Reveal answer
The variable is assigned the value `undefined`. — If the property key is missing, JavaScript simply returns `undefined` for that variable.
Are nested objects deeply copied when you destructure them out of a parent object?
Reveal answer
No, destructuring is a shallow assignment; objects are copied by reference. — Destructuring only unpacks the references. If you extract an object or an array, modifying it will still affect the original object's nested properties.
Challenges
🐞 Bug Hunt+25 XP
A developer is trying to pull the title and author out of the book object, but their code prints `undefined undefined`. Fix the syntax to correctly extract `title` and `author` from `book`.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it — the tests below decide when you are right.
javascript
Test 1 — expects "Dune Herbert\n"
Need a hint? (−25% XP)
You are using square brackets `[]` which are for array destructuring. Use curly braces `{}` for object destructuring.