# Destructuring, Spread & Rest

> Write cleaner, more modern JavaScript. Learn how to unpack objects and merge arrays using elegant syntax shortcuts.

Subject: JavaScript | Lesson 20 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/destructuring-and-spread
Previous: [References, Copying & structuredClone](https://codewithanimation.in/javascript/object-references-and-copying.md) | Next: [Object Methods & Property Descriptors](https://codewithanimation.in/javascript/object-methods-and-descriptors.md)

## What is Destructuring?

Destructuring is a syntax that allows you to unpack properties from objects (or items from arrays) into distinct, standalone variables.

Instead of writing `const name = user.name; const age = user.age;`, you can write it all in one line: `const { name, age } = user;`

## The Spread Operator (...)

The Spread operator looks like three dots (`...`). It allows an iterable (like an array or object) to be expanded in places where zero or more arguments are expected.

It is incredibly useful for merging data. `const newObj = { ...obj1, ...obj2 }` takes all properties from both objects and combines them into a brand new object.

## The Rest Parameter (...)

The Rest parameter looks exactly the same (`...`), but it does the exact opposite. While Spread expands things out, Rest collects things up.

In a function signature `function add(first, ...others)`, the `others` variable collects all remaining arguments into a neat array.

## Common mistake: Overwriting properties

When spreading objects together (`{ ...defaults, ...options }`), order matters! The object on the right will overwrite any matching keys from the object on the left. Always put your 'overrides' last.

## Tip: Renaming during destructuring

You can rename variables as you unpack them using a colon: `const { name: fullName } = user;`. This extracts `user.name` but assigns it to a new variable called `fullName`.

## Try it: The Syntax Sandbox

Select a syntax feature (Destructuring, Spread, or Rest) to see how data is dynamically unpacked or merged.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/destructuring-and-spread

## Check yourself

### Question 1

What does the Spread operator (`...`) do?

- A. It expands an array or object into its individual elements.
- B. It deletes properties from an object.
- C. It pauses the execution of a function.

Correct answer: A

Spread 'spreads out' the contents of an array or object, making it perfect for shallow copying or merging data structures.

- Why not B: Incorrect. The `delete` keyword does that.
- Why not C: Incorrect. That is the `yield` or `await` keyword.

### Question 2

If you want to extract `user.id` but call the variable `userId`, how do you write the destructuring?

- A. const { id: userId } = user;
- B. const { userId = id } = user;
- C. const { userId: id } = user;

Correct answer: A

You use a colon to rename during destructuring: `{ originalKey: newVariableName }`.

- Why not B: Incorrect. The equals sign sets a default value, it does not rename the variable.
- Why not C: Incorrect. The original property name must come first, followed by a colon and the new variable name.

### Question 3

What is the difference between Spread and Rest if they both use three dots (`...`)?

- A. Spread expands elements; Rest collects multiple elements into a single array.
- B. Spread is for arrays; Rest is for objects.
- C. Spread is used in functions; Rest is used in loops.

Correct answer: A

Context is everything. If the dots are on the receiving end (like a function parameter or inside a destructuring assignment), it's Rest (collecting). If they are used to pass data in or build a new object/array, it's Spread (expanding).

- Why not B: Incorrect. Both can be used with arrays and objects.
- Why not C: Incorrect. That is not how they are divided.

## Remember this

- Destructuring unboxes properties or array items into distinct variables.
- Spread (`...`) expands an iterable into individual elements.
- Rest (`...`) collects remaining elements into an array.
- When spreading objects, keys on the right overwrite matching keys on the left.
