How to Use the Spread and Rest Operators in JavaScript

🚀 ... — The Tiny Operator That Does Double Duty! Rest & Spread In JavaScript, a single operator ... serves two powerful roles based on where it’s used 👇 If it’s on the Left — it Gathers. Else, on the Right — it Scatters. 🔹 Spread Operator ➡ Purpose: Expands (or spreads) elements of an array or object into individual items. ➡ Used on the right side of =. const arr1 = [10, 20, 30]; const arr2 = [...arr1, 40, 50]; // arr2 → [10, 20, 30, 40, 50] 🔹 Rest Operator ➡ Purpose: Collects (or bundles) remaining elements into a single variable. ➡ Used on the left side of = during destructuring. const arr = [10, 20, 30, 40]; const [a, b, ...rest] = arr; // a = 10, b = 20, rest = [30, 40] 💭 Quick Tip: ... on the right → Spread ... on the left → Rest Both simplify working with collections and make code cleaner and more readable ✨ #JavaScript #WebDevelopment #CodingTips #JSOperators #SpreadOperator #RestOperator #LearnJavaScript #FrontendDevelopment #WebDevCommunity #TechLearning

To view or add a comment, sign in

Explore content categories