Course contentsShow
← JavaScript
Lesson 955 of 2,72321. DestructuringFree lesson

Rest Properties in Destructured Parameters

Combine destructuring with rest syntax to capture remaining properties as a single object.

Rest Properties in Destructured Parameters

What you'll learn: How to use the rest operator (...) within destructured function parameters to capture leftover properties into a single object.

The Core Idea

You already know how to destructure objects in function parameters and how to use rest properties in regular destructuring. Now you can combine both techniques: extract specific properties you need from a parameter object, then gather all remaining properties into one "catch-all" object using the rest operator.

This is incredibly useful when you want to extract a few specific properties for special handling, while passing the rest forward to another function or component.

How It Works

function createUser({ name, email, ...otherDetails }) {
  console.log(name);          // "Alice"
  console.log(email);         // "alice@example.com"
  console.log(otherDetails);  // { age: 30, country: "USA" }
}

createUser({ 
  name: "Alice", 
  email: "alice@example.com", 
  age: 30, 
  country: "USA" 
});

The ...otherDetails captures everything except name and email. Think of it like sorting mail: you pull out the urgent letters (name and email) and put everything else in one pile (otherDetails).

Practical Use Case

function logRequest({ method, url, ...headers }) {
  console.log(`${method} ${url}`);
  console.log("Headers:", headers);
}

logRequest({ 
  method: "GET", 
  url: "/api/users", 
  Authorization: "Bearer token123",
  ContentType: "application/json" 
});
// GET /api/users
// Headers: { Authorization: "Bearer token123", ContentType: "application/json" }

Key Takeaway: Use rest properties in destructured parameters to extract specific values while capturing all remaining properties in a single object, perfect for forwarding data or separating concerns.