✨ Understanding REST and SPREAD Operators in JavaScript ✨
Hello amazing readers! 👋
Today, we are going to learn about two magic dots in JavaScript:
REST (...
) and SPREAD (...
) operators.
Yes, both use three dots ...
, but they behave differently depending on where and how they are used.
Let’s break it down in a super easy way and relate it with real-life things so anyone, even a beginner, can understand. 🎯
🧺 Think of It Like a Bag...
Imagine you are going shopping 🛒
- You have many fruits 🍎🍌🍇🍊
- Sometimes you want to pack them together in one bag → like REST 🧺
- And sometimes you want to take fruits out from the bag and spread them on the table → like SPREAD 🍽️
Simple, right?
Let’s look deeper 🔍
🔁 REST Operator ...
(Collect things into one)
📦 What is it?
REST is used to collect multiple elements and pack them into a single array or object.
We usually use REST in functions to accept multiple arguments.
💡 Real-life example:
Imagine you are a delivery person. A customer orders many items. Instead of writing each item separately, you collect all of them in one box and deliver.
function deliverItems(...items) {
console.log("Delivering these items:", items);
}
deliverItems("Shoes", "Bag", "Watch", "Phone");
// Output: Delivering these items: [ 'Shoes', 'Bag', 'Watch', 'Phone' ]
➡️ Here, ...items
is REST operator. It collects all given values into one array.
🧑💻 Another Example: REST in Destructuring
const [first, second, ...others] = [1, 2, 3, 4, 5];
console.log(first); // 1
console.log(second); // 2
console.log(others); // [3, 4, 5]
➡️ others
is REST → collecting the remaining items.
🌟 SPREAD Operator ...
(Spread things out)
📦 What is it?
SPREAD is used to expand or spread the values of an array or object.
We use it when we want to copy, combine, or pass values one by one.
💡 Real-life example:
You have a box of chocolates 🍫
You want to put each chocolate on the table → so you open the box and spread them.
const chocolates = ["Dairy Milk", "KitKat", "5 Star"];
const moreChocolates = [...chocolates, "Munch", "Perk"];
console.log(moreChocolates);
// Output: [ 'Dairy Milk', 'KitKat', '5 Star', 'Munch', 'Perk' ]
➡️ SPREAD is opening the chocolates
array and spreading its values inside a new array.
🧑💻 Another Example: Copying an Array
const original = [1, 2, 3];
const copy = [...original];
console.log(copy); // [1, 2, 3]
Without SPREAD, doing this directly (const copy = original
) will point to the same memory.
🧑🍳 Example: Combine Objects
const person = { name: "Himanay", city: "Solna" };
const job = { role: "Frontend Developer" };
const fullInfo = { ...person, ...job };
console.log(fullInfo);
// Output: { name: 'Himanay', city: 'Solna', role: 'Frontend Developer' }
➡️ SPREAD takes properties from both objects and makes one combined object.
🔄 REST vs SPREAD Summary Table
Feature | REST ...
|
SPREAD ...
|
---|---|---|
📦 Purpose | Collect multiple values into one variable | Spread values from an array or object |
🧠 Use In | Function parameters, destructuring | Function calls, array/object copy or merge |
🔁 Direction | Gathers items | Spreads items |
🛠️ Example | function(...args) {} |
[...array] or {...object}
|
🧠 Final Real-Life Recap
- REST → Like putting many clothes into one suitcase 👕👖👗 → 🧳
- SPREAD → Like opening the suitcase and taking out all clothes one by one
✍️ Practice Time
Try writing a function using REST and call it with different arguments.
Then try copying or merging arrays using SPREAD. This will help you remember better! 🧑💻
🎯 Conclusion
- REST and SPREAD are both powerful and easy once you understand the context.
- Same syntax (
...
) but different use. - REST → collect
- SPREAD → expand
These are very useful in modern JavaScript. Mastering them will improve your coding a lot 🚀
Hope this blog helped you understand REST and SPREAD operators in a fun and simple way! 😊
Happy coding! 💻✨