Absolutely! Let’s break down the Spread and Rest operators in JavaScript, and explain the differences between them — in both English and Bangla.
✅ Spread & Rest Operators in JavaScript
Both Spread (...
) and Rest (...
) operators use the same three-dot syntax, but their purpose is different depending on the context.
🔹 1. Spread Operator (...
)
📘 English:
- The spread operator is used to expand elements of an array or object.
- It “spreads out” the values from an iterable (like an array) into individual elements.
📗 Bangla:
- স্প্রেড অপারেটর ব্যবহার করা হয় একটি array বা object-এর উপাদানগুলো আলাদা করে প্রকাশ করার জন্য।
- এটি একটি array বা object কে “ছড়িয়ে” দেয়, মানে তার উপাদানগুলোকে আলাদা করে দেয়।
🧠 Example:
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
console.log(newNumbers); // [1, 2, 3, 4, 5]
📌 Bangla: এখানে ...numbers
দিয়ে আগের array-এর উপাদানগুলো নতুন array-তে ছড়িয়ে (spread) দেওয়া হয়েছে।
Another Example with Object:
const person = { name: "Ali", age: 25 };
const updatedPerson = { ...person, city: "Dhaka" };
console.log(updatedPerson);
// { name: "Ali", age: 25, city: "Dhaka" }
🔸 2. Rest Operator (...
)
📘 English:
- The rest operator collects multiple elements into a single array or object.
- It’s used in function parameters or destructuring to gather the “rest” of the values.
📗 Bangla:
- রেস্ট অপারেটর ব্যবহার করা হয় একাধিক মানকে একত্র করে একটি array বা object এ রাখার জন্য।
- এটি বিশেষ করে function parameters বা destructuring-এ ব্যবহৃত হয়।
🧠 Example:
function sum(...args) {
return args.reduce((a, b) => a + b);
}
console.log(sum(1, 2, 3, 4)); // 10
📌 Bangla: এখানে ...args
সব প্যারামিটারকে একটি array তে নিয়ে আসছে — এইটাই rest operator।
Another example:
const [first, ...rest] = [10, 20, 30, 40];
console.log(first); // 10
console.log(rest); // [20, 30, 40]
🔁 Difference Between Spread and Rest Operators
Aspect | Spread Operator | Rest Operator |
---|---|---|
📘 Purpose (English) | Expands elements | Collects elements |
📗 উদ্দেশ্য (Bangla) | উপাদানগুলো ছড়িয়ে দেয় | উপাদানগুলো একত্র করে |
🔍 Used in | Array/Object cloning, function calls | Function parameters, destructuring |
📥 Input/Output | Takes array/object and spreads into individual items | Takes individual items and groups them into array/object |
💡 Example | const arr2 = [...arr1] |
function test(...args) {} |
✅ Summary:
Operator | বাংলা নাম | কাজ | উদাহরণ |
---|---|---|---|
... (Spread) |
স্প্রেড অপারেটর | মানগুলো ছড়িয়ে দেয় | const arr = [...oldArr] |
... (Rest) |
রেস্ট অপারেটর | মানগুলো একত্র করে | function f(...args) {} |