Rest Vs Spread Operator

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] into 1, 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 and b 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

FeatureRest (...)Spread (...)
Used inFunction params, destructuringFunction calls, cloning, merging
DirectionGathers into 1 itemBreaks into many items
Data TypeConverts into arrayExpands from array/object
Common UseCapture extra argsClone/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

SituationIs it Rest or Spread?Why?
Inside function parameters🟒 RestIt’s collecting arguments
Inside array/object destructuring (left side of =)🟒 RestIt’s collecting leftover values
Inside function callsπŸ”΅ SpreadIt’s spreading elements as args
Inside array/object creation (right side of =)πŸ”΅ SpreadIt’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 ...:

  1. Is it Rest or Spread?
  2. Why is it used here?
  3. 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

Leave a Comment

Your email address will not be published. Required fields are marked *