Understanding Spread and Rest Operators in JavaScript
Modern JavaScript introduces powerful features that make code cleaner and easier to work with. Two such features are the spread operator (...) and the rest operator (...).
At first, they may look identical because they use the same syntax, but they serve different purposes.
In this article, we will explore:
What the spread operator does
What the rest operator does
The difference between spread and rest
Using spread with arrays and objects
Practical use cases
All explanations are simple and focused on real-world usage.
The Core Idea: Expanding vs Collecting
The easiest way to understand these operators is:
Spread (
...) → expands valuesRest (
...) → collects values
What is the Spread Operator?
The spread operator is used to expand elements of an array or object.
Example with Array
let numbers = [1, 2, 3];
let newNumbers = [...numbers, 4, 5];
console.log(newNumbers);
Output:
[1, 2, 3, 4, 5]
Here:
...numbersspreads all elements into a new array
Spread Visualization
[1, 2, 3]
↓ spread
1, 2, 3
It breaks the array into individual elements.
Using Spread with Arrays
1. Copying an Array
let arr1 = [1, 2, 3];
let arr2 = [...arr1];
console.log(arr2);
2. Merging Arrays
let a = [1, 2];
let b = [3, 4];
let result = [...a, ...b];
console.log(result);
Output:
[1, 2, 3, 4]
Using Spread with Objects
Spread also works with objects.
let person = {
name: "Shivam",
age: 21
};
let updatedPerson = {
...person,
city: "Mumbai"
};
console.log(updatedPerson);
Output:
{ name: "Shivam", age: 21, city: "Mumbai" }
What is the Rest Operator?
The rest operator is used to collect multiple values into a single variable.
Example with Function
function sum(...numbers) {
console.log(numbers);
}
sum(1, 2, 3, 4);
Output:
[1, 2, 3, 4]
Here:
...numberscollects all arguments into an array
Rest Visualization
1, 2, 3, 4
↓ rest
[1, 2, 3, 4]
It gathers values into a single structure.
Rest with Arrays (Destructuring)
let numbers = [1, 2, 3, 4];
let [first, ...rest] = numbers;
console.log(first);
console.log(rest);
Output:
1
[2, 3, 4]
Differences Between Spread and Rest
| Feature | Spread Operator | Rest Operator |
|---|---|---|
| Purpose | Expands values | Collects values |
| Usage | Arrays, objects, function calls | Function parameters, destructuring |
| Direction | Breaks into pieces | Combines into one |
Practical Use Cases
1. Cloning Data
let original = [1, 2, 3];
let copy = [...original];
Useful to avoid modifying original data.
2. Combining Data
let fruits = ["apple", "banana"];
let moreFruits = ["mango", "orange"];
let all = [...fruits, ...moreFruits];
3. Passing Multiple Arguments
let nums = [1, 2, 3];
function add(a, b, c) {
return a + b + c;
}
console.log(add(...nums));
4. Handling Variable Arguments
function multiply(...nums) {
let result = 1;
for (let num of nums) {
result *= num;
}
return result;
}
console.log(multiply(2, 3, 4));
5. Updating Objects
let user = {
name: "Shivam",
age: 21
};
let updatedUser = {
...user,
age: 22
};
Spread vs Rest in One Example
function demo(a, b, ...rest) {
console.log(a, b);
console.log(rest);
}
let arr = [1, 2, 3, 4, 5];
demo(...arr);
Explanation:
...arr→ spreads values...rest→ collects remaining values
Assignment (Practice Exercise)
Try these in your console.
1) Merge Two Arrays
let a = [1, 2];
let b = [3, 4];
let result = [...a, ...b];
2) Use Rest in Function
function sum(...nums) {
return nums.reduce((acc, val) => acc + val, 0);
}
console.log(sum(1, 2, 3, 4));
3) Destructure with Rest
let arr = [10, 20, 30, 40];
let [first, ...rest] = arr;
console.log(first);
console.log(rest);
Final Thoughts
The spread and rest operators are simple but extremely powerful features in modern JavaScript. They make code more flexible, readable, and easier to manage.
By understanding:
how spread expands values
how rest collects values
where each one is used
you gain tools that are widely used in real-world applications and interviews.
The key idea to remember is simple:
Spread breaks things apart, rest gathers them together.
Once this concept becomes clear, using them in your code will feel natural and intuitive.