First Principles
Programming

Spread Operator in JavaScript

How the JavaScript spread operator works — merging arrays, copying objects, passing arguments, the shallow-copy trap, and immutable state updates.

By Fadjar Rafi. Published 16 Jun 2024. 3 min read.

Contents
  1. Arrays
  2. Function arguments
  3. Objects
  4. Sets and Maps
  5. Spread vs rest
  6. The shallow-copy trap
  7. Where it shines: immutable updates
  8. A couple of caveats

The spread operator (...) expands an iterable — an array, a string, or an object — into its individual elements. It arrived with ES6 and quickly became one of those tools you reach for every day: merging arrays, copying objects, or passing a list of arguments, all without touching the original data.

Arrays#

// Merge two arrays
const fruits = ['apple', 'banana']
const vegetables = ['carrot', 'tomato']
const combined = [...fruits, ...vegetables]
// ['apple', 'banana', 'carrot', 'tomato']

// Copy an array
const original = [1, 2, 3]
const copy = [...original]

The dots "unpack" each array so its items land side by side in the new one:

Two arrays being spread into a single combined array

Function arguments#

Spread an array into the separate arguments a function expects:

const numbers = [1, 2, 3]
Math.max(...numbers) // same as Math.max(1, 2, 3)

Objects#

const user = { name: 'John', age: 30 }
const withLocation = { ...user, location: 'New York' }
// { name: 'John', age: 30, location: 'New York' }

Order matters. Later properties override earlier ones, which is perfect for merging defaults with overrides:

const defaults = { theme: 'light', notifications: true }
const prefs = { theme: 'dark' }

const settings = { ...defaults, ...prefs }
// { theme: 'dark', notifications: true }

Flip the order and the defaults win instead.

Sets and Maps#

Since both are iterable, spread turns them back into arrays — a quick way to drop duplicates:

const unique = [...new Set([1, 2, 2, 3, 3, 4])]
// [1, 2, 3, 4]

const map = new Map([['a', 1], ['b', 2]])
const entries = [...map]
// [['a', 1], ['b', 2]]

Spread vs rest#

Same three dots, opposite jobs. Spread breaks a collection apart into items; rest gathers items back into an array. You can tell them apart by where they sit — rest lives in a function's parameter list, spread goes everywhere else.

// rest: gather
function collect(...args) {
  return args
}
collect(1, 2, 3) // [1, 2, 3]

// spread: expand
const numbers = [1, 2, 3]
console.log(...numbers) // 1 2 3

The shallow-copy trap#

This is the one that catches people. Spread copies a single level. Nested objects are still shared by reference, so changing them changes the original too.

const nested = { a: { b: 1 } }
const copy = { ...nested }

copy.a.b = 2
nested.a.b // 2 — the original changed

When you need a true deep copy, use structuredClone:

const copy = structuredClone(nested)

(JSON.parse(JSON.stringify(...)) works too, but it drops functions, Date objects, and anything JSON can't represent.)

Where it shines: immutable updates#

State updates in React and Redux depend on returning a new object instead of mutating the old one. Spread keeps that clean:

function reducer(state, action) {
  switch (action.type) {
    case 'UPDATE_USER':
      return {
        ...state,
        user: { ...state.user, ...action.payload },
      }
  }
}

A couple of caveats#

  • For very large arrays (thousands of elements), concat() can be faster than spreading.
  • Spread ships in every modern browser. Supporting Internet Explorer means transpiling with Babel.

That's really it. Once merging, copying, and overriding all become one-liners, it's hard to go back — just keep the shallow-copy behavior in mind when your data is nested.