01 / 21

Destructuring
Parameters

Learn to unpack object and array properties directly in function parameters for cleaner, more expressive JavaScript.

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.

JS
// 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.

✕ Without
function greet(user) {
  console.log(user.name);
}
✓ With
function greet({ name }) {
  console.log(name);
}

Where You'll See It Everywhere

React
Every component destructures props in its parameter list.
Express
Route handlers destructure req.body and req.params.
APIs
Callbacks from fetch, event listeners, and config objects all benefit.

The Core Syntax Pattern

function greet ( { name, age } ) { console.log(name, age); } Destructuring pattern replaces a single parameter CALL SITE greet({ name: "Ada", age: 36 });

Object vs Array Destructuring

Object
// Match by property name
function show({ title, year }) {
  console.log(title, year);
}
show({ title: "JS", year: 2015 });
Array
// 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.

JS
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.

JS
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

1
Identify the object parameter. Find the function parameter that receives an object (often called options, config, or props).
2
List the properties you need. Check which keys you actually use inside the function body.
3
Replace the parameter with { ... }. Write the property names inside curly braces in the parameter position.
4
Add defaults if needed. Use = value after any property that might be missing.
5
Remove dot-access lines. Delete the old param.key assignments: the variables already exist.

Example 1: Simple Refactor

Before
function displayProduct(product) {
  const name  = product.name;
  const price = product.price;
  return `${name}: $${price}`;
}
After
function displayProduct({ name, price }) {
  return `${name}: $${price}`;
}

displayProduct({ name: "Laptop", price: 999 });
// "Laptop: $999"

Example 2: Defaults + Rename

JS
function fetchData({
  url,
  method = "GET",
  headers: hdrs = {}
}) {
  console.log(url, method, hdrs);
}

fetchData({ url: "/api/users" });
// "/api/users" "GET" {}

Example 3: Nested Destructuring

JS
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.

JS
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

✕ Forgetting the Object
Calling greet("Ada") instead of greet({ name: "Ada" }). The function expects an object, not a raw string.
✕ Wrong Bracket Type
Using [ ] for objects or { } for arrays. The bracket type must match the data structure.
✕ Misspelling a Key
Writing { 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?

Question 5 of 5

What does rest contain after this call?
function f({ a, ...rest }) { return rest; }
f({ a: 1, b: 2, c: 3 })