Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Spread and rest syntax (...)

The `...` notation has two context-dependent roles: spread expands values or properties, while rest collects remaining arguments, elements, or properties. It is not a standalone expression operator. Copies made with this syntax are shallow.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

fn(...iterable)
[...iterable]
({ ...source })
function fn(first, ...args) {}
const [first, ...tail] = iterable;
const { key, ...remaining } = source;

... denotes spread or rest syntax depending on its position; it is not a standalone expression operator. Spread in fn(...iterable) passes values as arguments, while [...iterable] places them in a new array. The source must be iterable: a plain object is not enough. Very large argument expansions can exceed an engine's limit.

Spread in { ...source } copies own enumerable properties, including Symbol keys; it does not require iterability. Later definitions overwrite earlier values for the same key. It does not copy the prototype or property descriptors, and reading properties can invoke getters. Sources null and undefined are ignored here, but [...null] throws TypeError.

Rest in function fn(first, ...args) {} collects remaining arguments into a real array. In destructuring, const [first, ...tail] = iterable collects remaining iterator values into an array, while const { key, ...remaining } = source creates an object containing the remaining own enumerable properties. Rest must be last in its list and cannot have a trailing comma; a rest parameter cannot have a default value.

Copies are shallow: nested objects remain shared. The example demonstrates expansion, collection, and shared settings. Rest parameters, array rest, and array/argument spread date from ES2015; object rest/spread dates from ES2018. These are ECMAScript features, not Web APIs. The specification is not a browser compatibility table; support in a particular browser must be checked separately, and Node 24 in a repository does not prove it.

Examples

function sum(...values) {
  return values.reduce((total, value) => total + value, 0);
}

const numbers = [2, 3, 4];
const [first, ...tail] = numbers;
const original = { name: "Ada", settings: { active: true } };
const copy = { ...original, name: "Lin" };
const { name, ...remaining } = copy;

console.log(sum(...numbers));
console.log(JSON.stringify([first, ...tail, 5]));
console.log(name, remaining.settings === original.settings);

Result

9
[2,3,4,5]
Lin true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources