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
