ES6+ Modern JavaScript

Spread & Rest

The ... syntax does two opposite things depending on context: spread expands an iterable into individual elements, while rest collects multiple elements into a single array.

What is the difference between spread and rest in JavaScript?

Both use ..., but they work in opposite directions. Spread expands an array or object into individual items: [...a, ...b] merges arrays and { ...defaults, ...options } merges objects. Rest collects several items into one array or object: function sum(...nums) or const { id, ...others } = obj.

const merged = [...arr1, ...arr2];
const copy = { ...user, name: "New" };       // later keys win
Math.max(...[3, 9, 2]);                      // 9

function log(first, ...others) { … }         // rest parameter
const { password, ...safeUser } = user;      // rest in destructuring

Spread — Expand

Spread expands an iterable (array, string, Set, Map…) into individual elements. Used in array literals, object literals, and function calls.

Array clone[...arr]
Merge arrays[...a, ...b]
Spread in callfn(...args)
Object clone{ ...obj }
Object merge{ ...defaults, ...override }

Rest — Collect

Rest collects multiple values into a single array. Used in function parameters and destructuring — always the last element.

Variadic functionfunction f(...args) {}
Mixed paramsfunction f(a, b, ...rest) {}
Array restconst [h, ...tail] = arr
Object restconst { a, ...rest } = obj
Omit keyfunction omit({ pass, ...u }) { return u; }

Immutable Update Patterns

Spread is the foundation of React/Redux-style immutable state updates. Instead of mutating state directly, create a new object/array with the changes spread in.

Update a field

const next = { ...state, count: state.count + 1 };

Add to array

const next = [...items, newItem];

Remove from array

const next = items.filter(x => x.id !== id);

Shallow copy warning: Spread only clones the top level. Nested objects are still shared references. For deep cloning use structuredClone(obj) (modern) or a library like Lodash's _.cloneDeep.

Common Pitfalls

Thinking spread does a deep clone
Spread is shallow — nested objects are shared references. Use structuredClone() for deep cloning.
Spreading non-iterables: {...number} or [...null]
Only spread iterables (arrays, strings, Maps, Sets, generators). Objects can be spread in {} context only.
Rest parameter not being the last parameter: function f(...a, b)
Rest must always be the final parameter. SyntaxError otherwise.
Spreading a large array into a function (too many args)
JS engines have argument limits. For huge arrays, use a loop or reduce instead of spreading into a function call.

Interactive Examples

Code Editor

<pre id="out"></pre>

Preview

Spread and rest — step by step

1 / 4
1

Spread — expanding iterables

The spread operator (...) expands an iterable (array, string, Set, Map) into individual elements. [...arr1, ...arr2] creates a shallow copy of both arrays merged. {...obj1, ...obj2} merges objects (later keys overwrite earlier ones). [..string] splits a string into characters — correctly handles Unicode emoji and multi-codepoint characters unlike split('').

1 of 4
💡Prefer spread over Object.assign for clarity
{ ...source } is equivalent to Object.assign(, source) but more readable and composable. Unlike Object.assign, spread does not trigger setters on the target. Both create shallow copies. For arrays, [...arr] is cleaner than arr.slice() or Array.from(arr) for simple copies.

🎯 Quick Check

What does const b = { ...a } do if a has nested objects?

Spread and Rest: summary

Key points

  • Spread makes shallow copies: nested objects are still shared.
  • In object spread, properties on the right override those on the left.
  • The rest parameter must be the last parameter.

Frequently asked questions

›How do I copy an array in JavaScript?

Use [...arr] or arr.slice() for a shallow copy, and structuredClone(arr) for a deep copy.