
What They Have in Common
Both Rest and Spread use the three dots (...
) syntax.
But:
- π Rest: Combines multiple values into one (used in function parameters or destructuring).
- π Spread: Splits one value into multiple (used when calling functions or combining arrays/objects).
π§ REST OPERATOR (...rest
)
β Purpose: To gather remaining values into a single array.
It is mostly used in:
- Function parameters
- Object or array destructuring
πΉ Example 1: Rest in Function Parameters
function sum(...numbers) {
console.log(numbers);
}
sum(1, 2, 3, 4); // Output: [1, 2, 3, 4]
β Explanation:
...numbers
collects all arguments into an array.- You can now loop through or reduce them.
πΉ Example 2: Rest in Array Destructuring
const [first, ...rest] = [10, 20, 30, 40];
console.log(first); // 10
console.log(rest); // [20, 30, 40]
β Explanation:
first
takes the first item....rest
grabs everything else in the array.
π SPREAD OPERATOR (...spread
)
β Purpose: To spread (unpack) elements out of an array or object.
It is mostly used in:
- Function calls
- Combining arrays
- Copying objects/arrays
πΉ Example 1: Spread in Function Call
const numbers = [1, 2, 3];
Math.max(...numbers); // 3
β Explanation:
Math.max()
needs separate numbers....numbers
breaks[1, 2, 3]
into1, 2, 3
.
πΉ Example 2: Combine Arrays
const a = [1, 2];
const b = [3, 4];
const combined = [...a, ...b]; // [1, 2, 3, 4]
β Explanation:
- Spreads elements from
a
andb
into a new array.
πΉ Example 3: Copy and Extend Objects
const user = { name: "Darshan", age: 23 };
const updated = { ...user, city: "Kachchh" };
console.log(updated);
// { name: "Darshan", age: 23, city: "Kachchh" }
β Explanation:
- Spreads existing
user
object. - Adds new
city
property.
βοΈ Quick Comparison Table
Feature | Rest (... ) | Spread (... ) |
---|---|---|
Used in | Function params, destructuring | Function calls, cloning, merging |
Direction | Gathers into 1 item | Breaks into many items |
Data Type | Converts into array | Expands from array/object |
Common Use | Capture extra args | Clone/merge values |
π Mnemonic to Remember
- Rest = βRest of the itemsβ β puts them into an array.
- Spread = βSpread it outβ β pulls out items from a collection.
Even though both Rest and Spread use ...
, the meaning depends on where and how you’re using it in your code.
π§ HOW TO TELL: Is it Rest or Spread?
Letβs break it into 2 simple rules:
β Rule 1: Location in the code decides its role
Situation | Is it Rest or Spread? | Why? |
---|---|---|
Inside function parameters | π’ Rest | Itβs collecting arguments |
Inside array/object destructuring (left side of = ) | π’ Rest | Itβs collecting leftover values |
Inside function calls | π΅ Spread | Itβs spreading elements as args |
Inside array/object creation (right side of = ) | π΅ Spread | Itβs spreading to build new data |
β Rule 2: Left side = Rest, Right side = Spread
You can use this simple shortcut:
jsCopyEdit// Rest (LEFT side)
const [first, ...rest] = [1, 2, 3];
// Spread (RIGHT side)
const arr = [1, 2];
const newArr = [...arr, 3];
π Letβs look at some side-by-side examples:
β³οΈ Rest in function parameter:
function greet(...names) {
console.log(names);
}
greet("Ram", "Shyam"); // ['Ram', 'Shyam']
- β
...names
is gathering multiple arguments β REST
β³οΈ Spread in function call:
const nums = [10, 20];
console.log(Math.max(...nums)); // 20
- β
...nums
is spreading array into separate arguments β SPREAD
β³οΈ Rest in destructuring:
const [head, ...tail] = [1, 2, 3];
console.log(tail); // [2, 3]
- β
...tail
is on the left side β gathers β REST
β³οΈ Spread in array construction:
const start = [1];
const final = [...start, 2, 3];
console.log(final); // [1, 2, 3]
- β
...start
is on right side β spreads β SPREAD
π§ Final Shortcut
If you see ...
:
- On left side of
=
β REST - In function parameter β REST
- On right side of
=
or in function call β SPREAD
π§ͺ Exercise: Identify and Explain Rest or Spread
Read the following code and answer these 3 things for each line that has ...
:
- Is it Rest or Spread?
- Why is it used here?
- What is the final output?
jsCopyEditfunction displayInfo(name, ...skills) {
console.log(name);
console.log(skills);
}
const mainSkills = ["HTML", "CSS", "JavaScript"];
displayInfo("Darshan", ...mainSkills);
const user = {
name: "Darshan",
age: 23,
};
const updatedUser = {
...user,
city: "Kachchh",
};
console.log(updatedUser);
const [firstSkill, ...otherSkills] = ["React", "Node.js", "MongoDB"];
console.log(firstSkill);
console.log(otherSkills);
π― Your Tasks:
π For each of the ...
in the code above, answer:
- β Is it Rest or Spread?
- β Why?
- β What will be the printed output?
Thank You