JavaScript Spread and Rest Operators Explained

Day 19 / 40 – Frontend Learning Challenge Today I learned about the Spread (...) and Rest (...) Operators in JavaScript. At first, they look the same, but they serve different purposes depending on how they’re used. Spread Operator (...) The spread operator is used to expand elements from arrays or objects. Example with Arrays const nums = [1, 2, 3]; const newNums = [...nums, 4, 5]; console.log(newNums); // [1, 2, 3, 4, 5] Example with Objects const user = { name: "Alex", age: 22 }; const updatedUser = { ...user, role: "Frontend Developer" }; console.log(updatedUser); Useful for copying and merging data Rest Operator (...) The rest operator is used to collect multiple elements into one. Example with Function Parameters function sum(...numbers) { return numbers.reduce((total, num) => total + num, 0); } console.log(sum(1, 2, 3, 4)); // 10 Example with Arrays const [first, ...rest] = [10, 20, 30, 40]; console.log(first); // 10 console.log(rest); // [20, 30, 40] Useful when working with dynamic or unknown number of values Key Difference • Spread → Expands data • Rest → Collects data Why This Matters in Frontend Development These operators are widely used in: • React (props handling, state updates) • API data manipulation • Writing clean and concise code • Avoiding mutation of original data Day 19 complete — getting more comfortable with modern JavaScript #40DaysOfCode #JavaScript #FrontendDevelopment #WebDevelopment #LearningInPublic #Day10OfCoding #LearnToCode #HTML #CodingJourney #BuildInPublic #WebDesign #ProgrammingJourney

To view or add a comment, sign in

Explore content categories