Destructuring Assignment
Unpack values from arrays and objects into variables with concise, readable syntax.
What you'll learn
- Array destructuring by position
- Object destructuring by property name
- Default values and renaming variables
- Nested destructuring for deep structures
- Destructuring in function parameters
Concept
Array Destructuring
Array destructuring unpacks values by their position. You write a list of variables in square brackets on the left side of an assignment, and JavaScript maps them to the array elements in order. You can skip elements with empty commas and collect the rest with the ... rest pattern.
Object Destructuring
Object destructuring unpacks values by their property name. You write the property names in curly braces, and variables with the same name receive the values. The order does not matter because matching is by key, not position. This is the most common form of destructuring in modern code, especially when receiving function arguments or API responses.
Defaults and Renaming
If a property is undefined, you can provide a default value with the = operator. You can also rename the local variable with a colon: { propertyName: localName }. Combining renaming and defaults gives you { propertyName: localName = defaultValue }.
Nested Destructuring
Destructuring can reach into nested structures by mirroring the shape of the source. For an object like { user: { name } }, you write const { user: { name } } = data. This is powerful but can hurt readability if taken too many levels deep — flatten or destructure in two steps for deeply nested data.
In Function Parameters
Destructuring in function parameters is a clean way to accept many optional arguments without worrying about their order. Instead of function(opts) followed by opts.timeout, you write function({ timeout = 3000 }) and get named, defaulted parameters. This pattern is ubiquitous in modern libraries and React components.
Example
// Array destructuring by position, with skipping
const [, second, third] = ["a", "b", "c"];
console.log(second, third); // "b" "c"
// Rest pattern collects the remaining elements
const [first, ...rest] = [1, 2, 3, 4];
console.log(first, rest); // 1 [2, 3, 4]
// Object destructuring with renaming and defaults
const user = { name: "Ada", age: 36 };
const { name: fullName, role = "guest" } = user;
console.log(fullName, role); // "Ada" "guest"
// Nested destructuring
const response = {
data: { post: { title: "Hello", tags: ["js", "web"] } },
};
const {
data: {
post: { title, tags },
},
} = response;
console.log(title, tags); // "Hello" ["js", "web"]
// Destructuring in function parameters
function render({ title = "Untitled", items = [] } = {}) {
console.log(`#${title}`, items.length, "items");
}
render({ title: "My List", items: [1, 2] });
render(); // uses all defaults
// Swapping variables without a temp
let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b); // 2 1
Demonstrates array destructuring with skipping and rest, object destructuring with rename and default, nested destructuring, function parameter destructuring with defaults, and the classic variable swap.
Try it
- JSON Formatter
Inspect the objects and arrays you will destructure.
- JSON Tree Viewer
Browse nested structures before writing nested destructuring.
Common mistakes
The mistake
Using array destructuring syntax on an object or vice versa
The fix
Arrays use square brackets [a, b] = arr and match by position. Objects use curly braces { a, b } = obj and match by property name. Mixing them silently produces undefined values.
The mistake
Forgetting the outer parentheses when destructuring an already-declared variable
The fix
Without let/const, the leading { looks like a block statement. Wrap the assignment in parentheses: ({ name } = obj);. Prefer declaring with const at the same time to avoid this.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Destructuring patterns for arrays, objects, and function params.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.