Course contentsShow
← JavaScript
Lesson 984 of 2,72322. Spread and RestFree lesson

Rest in Object Destructuring

Apply rest syntax in object destructuring to gather remaining properties into a new object.

Rest in Object Destructuring

You'll learn how to capture leftover properties from an object into a new object using the rest operator during destructuring.

What Is Rest in Object Destructuring?

When destructuring an object, you can use the rest operator (...) to collect all remaining properties that weren't explicitly destructured into a new object. Think of it like sorting mail: you pick out a few specific letters by name, and everything else goes into a "miscellaneous" pile.

Basic Syntax

const user = {
  name: 'Alice',
  age: 30,
  city: 'Boston',
  job: 'Engineer'
};

const { name, age, ...otherInfo } = user;

console.log(name);      // 'Alice'
console.log(age);       // 30
console.log(otherInfo); // { city: 'Boston', job: 'Engineer' }

The ...otherInfo gathers all properties except name and age into a new object.

Why Use It?

This pattern is perfect when you want to extract a few specific properties and keep the rest grouped together:

const settings = {
  theme: 'dark',
  language: 'en',
  fontSize: 14,
  notifications: true
};

const { theme, ...preferences } = settings;
// theme → 'dark'
// preferences → { language: 'en', fontSize: 14, notifications: true }

Important Rules

  • Rest properties must come last in the destructuring pattern
  • The rest object is a shallow copy of the remaining properties
  • Only enumerable, own properties are collected

Key Takeaway: The rest operator in object destructuring (...) collects all undestructured properties into a new object, letting you cleanly separate specific values from "everything else."