Spread and rest use exactly the same syntax (...) — but they do opposite jobs depending on where you place them.
- Spread = expands (unpacks) values into individual elements.
- Rest = collects (packs) remaining values into a new array.
If you remember only this one rule — “left of = or in function call = spread (expand); right of = or in parameters = rest (collect)” — you’ll never confuse them again.
Hotel Analogy (so you’ll never forget Rest)
Imagine a hotel:
- The spread operator is like the bellboy unpacking a guest’s suitcase and laying every item separately on the bed (1 shirt, 1 pant, 1 shoe…).
- The rest operator is like the receptionist collecting all the extra guests who don’t have individual bookings and putting them together into one big “rest room” (an array).
Easy to remember forever.
1. What the Spread Operator Does
It takes an iterable (array, object, string, etc.) and expands it into individual elements.
const nums = [1, 2, 3];
// Spread in array literal
const newArr = [...nums, 4, 5];
console.log(newArr); // [1, 2, 3, 4, 5]
// Spread in function call
console.log(...nums); // 1 2 3 (not an array!)
2. What the Rest Operator Does
It collects the remaining values and packs them into a new array.
// Rest in function parameters
function sum(first, second, ...rest) {
console.log("First two:", first, second);
console.log("Rest collected:", rest);
}
sum(10, 20, 30, 40, 50);
// Output:
// First two: 10 20
// Rest collected: [30, 40, 50]
3. Differences Between Spread and Rest (Quick Comparison)
| Feature | Spread (...) |
Rest (...) |
|---|---|---|
| Job | Expands values | Collects values into array |
| Where it works | Right-hand side (literals, function calls) | Left-hand side (parameters, destructuring) |
| Creates new thing? | Yes (new array/object) | Yes (new array) |
| Common use | Copy, merge, pass arguments | Variable arguments, destructuring |
| Real-life analogy | Unpacking suitcase | Hotel collecting extra guests |
Golden Rule:
- If you see
...producing individual items → it’s spread. - If you see
...gathering items into one variable → it’s rest.
4. Using Spread with Arrays and Objects (Small Examples)
Arrays
const arr1 = [1, 2, 3];
const arr2 = [4, 5];
// Copy (shallow)
const copy = [...arr1]; // [1, 2, 3]
// Merge
const merged = [...arr1, ...arr2]; // [1, 2, 3, 4, 5]
// Insert in middle
const insert = [0, ...arr1, 99]; // [0, 1, 2, 3, 99]
Objects
const user = { name: "Bhupesh", age: 25 };
// Shallow copy + add new property
const updated = { ...user, city: "Bageshwar" };
// { name: "Bhupesh", age: 25, city: "Bageshwar" }
// Merge two objects (later properties override)
const settings = { theme: "dark", lang: "hi" };
const final = { ...user, ...settings };
// { name: "Bhupesh", age: 25, theme: "dark", lang: "hi" }
5. Practical Real-World Usage Patterns
Here are the patterns you’ll actually use every day in web development:
- Array cloning without mutation (super common)
const original = [1, 2, 3];
const clone = [...original]; // safe copy
clone.push(4); // original stays unchanged
-
Passing variable number of arguments (no more
argumentsobject)
function logAll(...messages) {
messages.forEach(msg => console.log(msg));
}
logAll("Error", "Warning", "Info");
- Math functions with dynamic arrays
const scores = [87, 92, 95, 88];
const highest = Math.max(...scores); // 95
- React / component props spreading
const props = { title: "Dashboard", isLoading: false };
<MyComponent {...props} extraProp="yes" />
- Destructuring with rest (clean data extraction)
const [first, second, ...remaining] = [10, 20, 30, 40, 50];
console.log(remaining); // [30, 40, 50]
const { name, age, ...otherInfo } = user;
Visual Diagram of the Core Idea
Spread (Expanding)
[1, 2, 3] → ...nums → 1 2 3 (individual elements)
Rest (Collecting)
function(a, b, ...rest) → collects → [30, 40, 50] (one array)
That’s it!
Once you internalize “spread expands, rest collects — same dots, opposite jobs,” these two operators become some of the most powerful and readable tools in modern JavaScript. Start using them in your next cohort assignment today — you’ll instantly write cleaner, more professional code.