Destructuring
Parameters
Learn to unpack object and array properties directly in function parameters for cleaner, more expressive JavaScript.
What Is Destructuring?
Destructuring is a JavaScript syntax that lets you extract values from arrays or properties from objects and assign them to variables in a single, concise statement.
// Without destructuring const obj = { name: "Ada", age: 36 }; const name = obj.name; const age = obj.age; // With destructuring const { name, age } = obj;
Why Destructure in Parameters?
Functions often receive objects as arguments. Instead of accessing properties inside the function body, you can unpack them right in the parameter list: shorter, more readable, and self-documenting.
function greet(user) {
console.log(user.name);
}function greet({ name }) {
console.log(name);
}Where You'll See It Everywhere
The Core Syntax Pattern
Object vs Array Destructuring
// Match by property name function show({ title, year }) { console.log(title, year); } show({ title: "JS", year: 2015 });
// Match by position function sum([a, b]) { return a + b; } sum([3, 7]); // 10
Name Matching vs Position Matching
| Object { } | Array [ ] | |
|---|---|---|
| Matches by | Property name | Index position |
| Order matters? | No | Yes |
| Skip elements? | Just omit the key | Use , , placeholder |
| Rename variable? | { a: b } |
Any name works |
| Caller passes | An object { } |
An array [ ] |
Adding Default Values
If a property is missing from the incoming object, the variable becomes undefined. Defaults let you provide a fallback.
function createUser({ name, role = "viewer", // default if missing active = true }) { console.log(name, role, active); } createUser({ name: "Ada" }); // "Ada" "viewer" true
Renaming Properties
Sometimes the property name in the object doesn't match the variable name you want inside the function. Use the colon syntax to rename.
function printCoords({ x: horizontal, // x → horizontal y: vertical // y → vertical }) { console.log(horizontal, vertical); } printCoords({ x: 10, y: 20 }); // 10 20
Step-by-Step Approach
options, config, or props).= value after any property that might be missing.param.key assignments: the variables already exist.Example 1: Simple Refactor
function displayProduct(product) { const name = product.name; const price = product.price; return `${name}: $${price}`; }
function displayProduct({ name, price }) { return `${name}: $${price}`; } displayProduct({ name: "Laptop", price: 999 }); // "Laptop: $999"
Example 2: Defaults + Rename
function fetchData({ url, method = "GET", headers: hdrs = {} }) { console.log(url, method, hdrs); } fetchData({ url: "/api/users" }); // "/api/users" "GET" {}
Example 3: Nested Destructuring
function showAddress({ name, address: { city, zip } }) { console.log(`${name} lives in ${city} ${zip}`); } showAddress({ name: "Ada", address: { city: "London", zip: "SW1" } }); // "Ada lives in London SW1"
Collecting the Rest
Use rest syntax (...rest) to gather all remaining properties you did not destructure into a new object.
function logUser({ id, ...rest }) { console.log("ID:", id); console.log("Other:", rest); } logUser({ id: 1, name: "Ada", age: 36 }); // ID: 1 // Other: { name: "Ada", age: 36 }
Common Mistakes
greet("Ada") instead of greet({ name: "Ada" }). The function expects an object, not a raw string.[ ] for objects or { } for arrays. The bracket type must match the data structure.{ nme } when the property is name. It silently produces undefined with no error.Summary Cheat Sheet
| Feature | Syntax | Result |
|---|---|---|
| Basic | ({ a, b }) |
Extracts a and b |
| Default | ({ a = 1 }) |
a is 1 if missing |
| Rename | ({ a: x }) |
a stored as x |
| Nested | ({ a: { b } }) |
Extracts b from a |
| Rest | ({ a, ...r }) |
r has everything else |
| Array | ([ a, b ]) |
Extracts by position |
Question 1 of 5
What does the following function signature do?function log({ message, level = "info" })
Question 2 of 5
Given function f({ x: a, y: b }) { return a + b; }, what does f({ x: 3, y: 7 }) return?
Question 3 of 5
What value does color hold after this call?function paint({ color = "red" }) { return color; }
paint({ color: null })
Question 4 of 5
Which line correctly destructures an array parameter to get the first two elements?
[a, b] and matches by position. Option D technically works but is not idiomatic array destructuring.Question 5 of 5
What does rest contain after this call?function f({ a, ...rest }) { return rest; }
f({ a: 1, b: 2, c: 3 })