JavaScript Tip: Spread & Rest Operators Explained

JavaScript Tip of the Day: Spread & Rest Operators ( … ) The ... (three dots) in JavaScript might look simple — but it’s super powerful! It can expand or collect values depending on how you use it. 🔹 Spread Operator Used to expand arrays or objects. const nums = [1, 2, 3]; const moreNums = [...nums, 4, 5]; console.log(moreNums); // [1, 2, 3, 4, 5] You can also use it for objects: const user = { name: "Venkatesh", role: "Engineer" }; const updatedUser = { ...user, location: "India" }; console.log(updatedUser); 🔹 Rest Operator Used to collect remaining arguments into an array. function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); } console.log(sum(10, 20, 30)); // 60 You can even use it in destructuring: const [first, ...rest] = [1, 2, 3, 4]; console.log(first); // 1 console.log(rest); // [2, 3, 4] 🔸 In Short: Spread → Expands data Rest → Collects data Same syntax, opposite purpose! 💬 Question for You Which operator do you use more often — Spread or Rest? Drop your answer 👇 and tag a friend learning JS! #JavaScript #Coding #WebDevelopment #LearnToCode #FrontendDevelopment #CodingCommunity #SoftwareEngineering #JavaScriptTips #JSDeveloper #ProgrammingLife #TechLearning #CodeNewbie #WebDevJourney #100DaysOfCode #ES6 #DeveloperCommunity #CodingInPublic #FullStackDeveloper #JSConcepts

To view or add a comment, sign in

Explore content categories