Spread vs Rest Operators in JavaScript: Expand or Collect Like a Pro

Published: (April 24, 2026 at 02:24 PM EDT)
3 min read
Source: Dev.to

Source: Dev.to

Introduction

Imagine you’re juggling arrays and objects in JavaScript, and you need to copy, merge, or slice them without breaking a sweat.

Enter the spread operator (...) and rest operator (...)—two syntactic superheroes that look identical but have different jobs. They’re game‑changers for clean, readable code in modern JS, especially in React apps and Node.js back‑ends.

  • Spread expands values into individual elements.
  • Rest collects values into a single array or object.

Let’s dive in with examples and see how they shine in real scenarios.

What the Spread Operator Does: Expanding Values

The spread operator (...) 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']
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.

spread operator

What the Rest Operator Does: Collecting Values

The rest operator (...) does the opposite: it gathers elements into a single array (or object). It works in function parameters and destructuring, 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])
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.

rest operator

Key Differences: Spread Expands, Rest Collects

AspectSpread (...)Rest (...)
DirectionExpands (unpacks)Collects (packs)
ContextAnywhere (arrays, objects, calls)Function parameters or destructuring
Use CaseCopying, merging, passing argsVariable args, destructuring leftovers
Example[...arr] clones an arrayfunction(...args) gathers arguments

Spread is for spreading values outward; rest is for gathering them inward. Confusing them can break your code—spread won’t work in parameter lists.

Practical Use Cases: Real‑World Wins

Cloning Arrays/Objects (Spread)

Avoid mutations in React state.

const state = { users: [] };
const newState = { ...state, users: [...state.users, newUser] };

Function Arguments (Spread)

Pass arrays dynamically.

const coords = [10, 20];
console.log(Math.max(...coords)); // 20

API Data Merging (Spread)

Combine defaults with user input.

const defaults = { method: 'GET' };
const config = { ...defaults, url: '/api/users' };
fetch(config.url, config);

Variable Arguments in Node.js Utils (Rest)

Build flexible helpers.

function log(...messages) {
  console.log(new Date(), ...messages);
}
log('User login:', userId, timestamp);

Destructuring Props (Rest) – React

function Button({ children, ...props }) {
  return {children};
}

These patterns appear everywhere—from full‑stack deployments on Vercel to interview coding rounds.

Wrapping Up: Master Both for Cleaner JS

Spread and rest operators turn clunky array/object operations into elegant one‑liners, saving you headaches in full‑stack development. Remember:

  • Spread expands for copying/merging.
  • Rest collects for flexible parameters.

Practice with small examples, and they’ll become second nature in your projects. Next time you refactor Node.js routes or React components, reach for ...—your future self will thank you.

0 views
Back to Blog

Related posts

Read more »

Master Destructuring in One Go

What Destructuring Means Destructuring is a JavaScript expression that lets you extract values from arrays, objects, maps, and sets in a concise way. Instead o...