Skip to main content

Command Palette

Search for a command to run...

Spread vs Rest Operators in JavaScript – Complete Guide with Examples

Updated
β€’4 min read
Spread vs Rest Operators in JavaScript – Complete Guide with Examples

JavaScript ES6 introduced two powerful features that often confuse beginners: the Spread (...) and Rest (...) operators.

They look exactly the sameβ€”but behave very differently depending on where and how you use them.

In this blog, you’ll learn everything you need:

  • What spread operator does

  • What rest operator does

  • Key differences

  • Real-world use cases

  • Easy examples to understand deeply


πŸ”Ή What is the Spread Operator?

The spread operator (...) expands elements of an array, object, or iterable into individual elements.

πŸ‘‰ Think of it as: "Unpacking" or "Expanding" values

βœ… Example with Arrays

const arr = [1, 2, 3];

const newArr = [...arr, 4, 5];

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

πŸ‘‰ Here, ...arr expands the array into individual elements.


βœ… Example with Objects

const user = {
  name: "Virat",
  age: 30
};

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

console.log(updatedUser);

πŸ‘‰ Output:

{
  name: "Vishal",
  age: 30,
  city: "Delhi"
}

πŸ“Œ Key Uses of Spread Operator

βœ” Copy arrays/objects βœ” Merge arrays/objects βœ” Pass multiple arguments to functions


βœ… Function Example

const numbers = [10, 20, 30];

function sum(a, b, c) {
  return a + b + c;
}

console.log(sum(...numbers)); // 60

πŸ”Ή What is the Rest Operator?

The rest operator (...) collects multiple elements into a single array.

πŸ‘‰ Think of it as: "Packing" or "Collecting" values


βœ… Example in Function Parameters

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

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

πŸ‘‰ Here, ...numbers collects all arguments into an array.


βœ… Example with Array Destructuring

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

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

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

πŸ“Œ Key Uses of Rest Operator

βœ” Handle variable number of arguments βœ” Collect remaining elements βœ” Simplify function parameters


πŸ”₯ Spread vs Rest: Key Difference

Feature Spread Operator Rest Operator
Purpose Expands values Collects values
Direction Inside β†’ Outside Outside β†’ Inside
Use Case Copy, merge, pass values Gather remaining values
Example [...arr] function(...args)

🧠 Core Concept (Most Important)

πŸ‘‰ Spread = Expand values πŸ‘‰ Rest = Collect values


🎯 Real-World Use Cases

1️⃣ Copying Arrays (Avoid Mutation)

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

copy.push(4);

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

2️⃣ Merging Arrays

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

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

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

3️⃣ Updating Objects (React Style)

const state = {
  name: "Virat",
  age: 30
};

const newState = {
  ...state,
  age: 21
};

πŸ‘‰ Used heavily in React for immutability.


4️⃣ Flexible Function Arguments

function multiply(multiplier, ...nums) {
  return nums.map(num => num * multiplier);
}

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

5️⃣ Removing Elements Using Destructuring

const numbers = [10, 20, 30, 40];

const [removed, ...remaining] = numbers;

console.log(remaining); // [20, 30, 40]

πŸ“Š Visual Understanding

πŸ”Ή Spread (Expanding)

[1, 2, 3]
   ↓
...arr
   ↓
1, 2, 3

πŸ”Ή Rest (Collecting)

1, 2, 3, 4
   ↓
...nums
   ↓
[1, 2, 3, 4]

⚠️ Common Mistakes

❌ Using rest in the wrong position

// ❌ Wrong
const [...rest, last] = [1, 2, 3];

βœ” Rest must always be last:

const [first, ...rest] = [1, 2, 3];

🧩 When to Use What?

Use Spread (...) when:

  • You want to expand values

  • You need to copy or merge data

Use Rest (...) when:

  • You want to collect values

  • You handle unknown number of arguments


🏁 Conclusion

Even though spread and rest operators look identical, their roles are completely opposite:

  • Spread β†’ Expands values

  • Rest β†’ Collects values

Mastering these will make your JavaScript code: βœ” Cleaner βœ” Shorter βœ” More powerful


More from this blog

Dev Blog by Vishal

36 posts

Spread vs Rest Operators in JavaScript – Complete Guide