
Introduction Imagine you're juggling arrays and objects in JavaScript, and suddenly you...
Imagine you're juggling arrays and objects in JavaScript, and suddenly you need to copy, merge, or slice them without breaking a sweat.
Entry of the spread operator (...) and rest operator (...)—two syntactic superheroes that look identical buthave different jobs. They're game-changers for clean, readable code in modern JS and provide perfect Developer's experience, especially in React apps, Node.js backend applications.
But what's the difference between these two?
Spread expands values into individual elements; Rest collects them into a single array or object
Let's dive in with examples and see how they shine (or clash) in real scenarios.
The spread operator (...) is like dropping a glass from a height and it spreads down completely with so many broken parts; this takes an iterable—like an array or object—and spreads (expands) its elements into individual items. It's like unpacking a box: everything spills out for easy use.
const fruits = ['apple', 'banana'];
const moreFruits = ['orange', ...fruits, 'grape'];
// Result: ['orange', 'apple', 'banana', 'grape']
Here, ...fruits expands the array, letting you merge without messy loops.
const user = { name: 'Ritam', city: 'Kolkata' };
const updatedUser = { ...user, age: 20, hobby: 'coding' };
// Result: { name: 'Ritam', city: 'Kolkata', age: 20, hobby: 'coding' }
Spread creates shallow copies and merges—perfect for immutable updates in state management.

The rest operator (...) does the opposite: it gathers (collects) elements into a single array. It only works in function parameters depending on the passed arguments and for destrcturing, acting like a "vacuum" for leftovers.
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
sum(1, 2, 3, 4); // Result: 10 (numbers = [1, 2, 3, 4])
Rest collects all arguments into numbers.
const [first, ...rest] = ['apple', 'banana', 'orange']; //Array destructuring
console.log(first); // 'apple'
console.log(rest); // ['banana', 'orange']
It grabs the first item, then collects the rest.

| Aspect | Spread (...) | Rest (...) |
|---|---|---|
| Direction | Expands (unpacks) | Collects (packs) |
| Context | Anywhere (arrays, objects, calls) | Function params or destructuring |
| Use Case | Copying, merging, passing args | Variable args, destructuring leftovers |
| Example | [...arr] clones array | function(...args) |
Spread is for spreading values outward; rest is for gathering them inward. Confuse them? Your code breaks—spread won't work in params!
const state = { users: [] };
const newState = { ...state, users: [...state.users, newUser] };
const coords = [10, 20];
console.log(Math.max(...coords)); // 20
const defaults = { method: 'GET' };
const config = { ...defaults, url: '/api/users' };
fetch(config.url, config);
function log(...messages) {
console.log(new Date(), ...messages);
}
log('User login:', userId, timestamp);
function Button({ children, ...props }) {
return <button {...props}>{children}</button>;
}
These patterns pop up everywhere—from your full-stack deploys on Vercel to interview coding rounds.
Spread and rest operators turn clunky array/object ops into elegant one-liners, saving you headaches in full-stack dev. Remember: spread expands for copying/merging; rest collects for flexible params. Practice with small examples, and they'll become second nature for your portfolio projects. Next time you're refactoring Node.js routes or React components, reach for ...—your future self will thank you. What's your favorite use case? Drop it in the comments!
aiMost of us have seen a coding agent fail to complete a task we know it can do. We just don't...
googlecloudWhen building Generative AI applications, developers often encounter a massive bottleneck: sequential...
discussI’ve been thinking about sharing some electronic circuit posts on Dev.to — small circuits, DIY...
agentsWhat nobody tells you about exporting your multi-agent prototype to a local workspace. Every...
agenticarchitectAutonomous agents are genuinely good at answering messy business questions. Give one an LLM and a set...
aiPR volume went up, ticket quality didn't, and the gap got filled with LLMs on both sides of the review: bots reviewing, bots replying, bots occasionally arguing with bots about priorities that only existed in a teammate's head. Our CEO named the actual problem, and it's bigger than code review.
Workflows from the Neura Market marketplace related to this Stable Diffusion resource