Skip to main content

Command Palette

Search for a command to run...

Understanding Spread and Rest Operators in Js

Updated
5 min read
Understanding Spread and Rest Operators in Js

Modern JavaScript introduced two incredibly powerful features that often confuse beginners at first — the spread operator (...) and the rest operator (...).

Yes, both use the same syntax (...), but they behave very differently depending on where and how they are used.

To truly understand them, you need one simple mental model:

👉 Spread = expand values 👉 Rest = collect values

In this guide, we’ll break everything down clearly with examples, comparisons, and real-world use cases.


What the Spread Operator Does

The spread operator takes an iterable (like an array or object) and expands it into individual elements.

Think of it like opening a box and spreading its contents out.


Basic Example with Arrays

const nums = [1, 2, 3];

console.log(...nums); // 1 2 3

Here:

[1, 2, 3] → 1 2 3

The array is expanded into individual values.


Using Spread to Copy Arrays

const original = [1, 2, 3];
const copy = [...original];

console.log(copy); // [1, 2, 3]

This creates a new array, not a reference.


Merging Arrays

const a = [1, 2];
const b = [3, 4];

const merged = [...a, ...b];

console.log(merged); // [1, 2, 3, 4]

Instead of:

a.concat(b)

Spread is cleaner and more readable.


Spread with Objects

const user = { name: "Rahul", age: 21 };

const updated = { ...user, city: "Delhi" };

console.log(updated);

Output:

{ name: "Rahul", age: 21, city: "Delhi" }

Overriding Values

const user = { name: "Rahul", age: 21 };

const updated = { ...user, age: 25 };

console.log(updated);

Output:

{ name: "Rahul", age: 25 }

👉 Later values override earlier ones.


What the Rest Operator Does

The rest operator collects multiple values into a single structure (array or object).

Think of it like packing items into a bag.


Rest in Function Parameters

function sum(...numbers) {
  return numbers.reduce((acc, num) => acc + num, 0);
}

console.log(sum(1, 2, 3, 4)); // 10

Here:

1, 2, 3, 4 → [1, 2, 3, 4]

All arguments are collected into numbers.


Rest in Array Destructuring

const arr = [1, 2, 3, 4];

const [first, ...rest] = arr;

console.log(first); // 1
console.log(rest);  // [2, 3, 4]

Rest in Object Destructuring

const user = {
  name: "Rahul",
  age: 21,
  city: "Delhi"
};

const { name, ...rest } = user;

console.log(name); // Rahul
console.log(rest); // { age: 21, city: "Delhi" }

Spread vs Rest — Key Difference

Even though both use ..., their behavior is opposite.

Feature Spread Rest
Purpose Expands values Collects values
Usage Arrays, objects, function calls Function params, destructuring
Direction One → many Many → one

Visualization

Spread (Expanding)

[1, 2, 3]
   ↓
1   2   3

Rest (Collecting)

1   2   3
   ↓
[1, 2, 3]

Practical Use Cases

Now let’s see how these are used in real-world scenarios.


1. Cloning Data (Immutability)

const state = { count: 0 };

const newState = { ...state, count: 1 };

Used heavily in:

  • React

  • Redux

  • State management


2. Merging API Data

const user = { name: "Rahul" };
const details = { age: 21, city: "Delhi" };

const full = { ...user, ...details };

3. Flexible Functions

function logAll(...args) {
  console.log(args);
}

logAll("a", "b", "c"); // ["a", "b", "c"]

4. Removing Properties

const user = {
  name: "Rahul",
  password: "1234"
};

const { password, ...safeUser } = user;

console.log(safeUser); // { name: "Rahul" }

5. Passing Arrays as Arguments

const nums = [1, 2, 3];

Math.max(...nums); // 3

Instead of:

Math.max(1, 2, 3)

Common Mistakes

1. Confusing Spread and Rest

function test(...args) {} // rest
console.log(...args);     // spread

👉 Same syntax, different roles.


2. Shallow Copy Issue

const obj = { nested: { a: 1 } };

const copy = { ...obj };

copy.nested.a = 2;

👉 This modifies original too (because it's shallow).


3. Using Rest Not at End

const [ ...rest, last ] = arr // ❌ ERROR

👉 Rest must be last element.


Real-World Analogy

Imagine:

Spread

You open a box and spread items on the table

📦 → 📄 📄 📄

Rest

You collect items and put them into a box

📄 📄 📄 → 📦

Conclusion

The spread and rest operators are among the most useful features in modern JavaScript. Even though they share the same syntax (...), their behavior is completely opposite. Spread expands values, making it perfect for copying, merging, and passing data. Rest collects values, making it ideal for handling variable inputs and destructuring.

Understanding this difference is crucial because these operators are used everywhere in modern JavaScript — from simple functions to complex frameworks like React. Once you master them, your code becomes cleaner, more flexible, and easier to maintain.

And the best part? With just one mental model — expand vs collect — you can always remember how they work.