Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Operators

Spread (...) and rest syntax

Reference for the JavaScript ... syntax: spreading arrays, strings and objects, collecting rest parameters, rest in destructuring, and shallow-copy pitfalls.

The three dots ... do two opposite jobs depending on where they appear. As spread, they expand an iterable (or an object's own enumerable properties) into individual items. As rest, they collect the remaining items into a new array (or object). The syntax is identical, so the position tells you which one you are looking at: in a call, array literal or object literal it is spread; in a parameter list or a destructuring pattern it is rest.

JavaScript
const merged = [...first, ...second];   // spread in an array literal
const copy = { ...original, extra: 1 }; // spread in an object literal
Math.max(...numbers);                   // spread in a call

function log(first, ...others) {}       // rest parameter
const [head, ...tail] = list;           // rest in array destructuring
const { id, ...fields } = record;       // rest in object destructuring

Try it

JS Spread: expand into a call, array and object

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Rest: collect what is left

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
f(...iterable)
[...iterable]
{ ...object }

function f(...rest) {}
[a, ...rest] = iterable
({ a, ...rest } = object)
Form Description
f(...iterable) Spread in a call: each value the iterable yields becomes a separate argument. Several spreads and plain arguments can be mixed.
[...iterable] Spread in an array literal: the values are inserted at that position.
{ ...object } Spread in an object literal: copies the source's own enumerable properties (strings and symbols) into the new object.
...rest parameter Collects all extra arguments into a real array. Must be the last parameter, and there can be only one.
...rest in destructuring Collects the remaining elements or own enumerable properties. Must be the last element in the pattern.

Return value: spread is not an expression by itself, so it has no value; it produces the items inside the surrounding call or literal. A rest element evaluates to a new array (array pattern) or a new plain object (object pattern).

Exceptions: spreading a non-iterable into a call or array literal throws a TypeError (for example [...5] or [...{a: 1}]). Spreading null or undefined into an object literal is allowed and adds nothing. A rest element that is not last is a SyntaxError.

Usage notes

  • Iterables vs objects. Array and call spread use the iteration protocol, so they work on arrays, strings, Sets, Maps, generators and typed arrays. Object spread does not iterate: it reads own enumerable properties, so it works on any object.
  • Shallow copies only. [...arr] and { ...obj } copy the top level; nested objects are shared. Use structuredClone() for a deep copy.
  • Order matters in objects. When two sources have the same key, the later one wins. This makes { ...defaults, ...overrides } a handy pattern.
  • Own properties only. Object spread skips inherited properties and non-enumerable ones, and it does not copy getters as getters: it reads the value and stores it as a plain data property.
  • Class instances lose their prototype. { ...instance } creates a plain object, so methods defined on the class are not copied.
  • Rest parameters are real arrays, unlike the old arguments object, so you can call map() and filter() on them directly. Arrow functions have no arguments but do support rest.
  • Large arrays in calls. Math.max(...hugeArray) can exceed the engine's argument limit and throw a RangeError; use reduce() or a loop for very big inputs.
  • Rest does not count in length. function f(a, ...b) {} has f.length === 1.

Browser support

Array and call spread and rest parameters are supported in every current browser (ES2015). Object spread and object rest are older than most "modern" syntax, too (ES2018), and also work in current browsers.