Spread Operator (JavaScript)

The spread operator (...) expands an array or object into its individual elements. It is used to copy, merge, or pass collections of values cleanly.

JavaScript

Spread lets you copy an array or object into a new one, combine several together, or pass an array's items as separate function arguments.

It creates a shallow copy, which is a common way to update state without mutating the original.

Syntax

[...array]   {...object}   fn(...args)

Examples

JavaScript

const a = [1, 2];
const b = [3, 4];
const combined = [...a, ...b];
console.log(combined);

Output:

[1, 2, 3, 4]

JavaScript

const base = { x: 1 };
const updated = { ...base, y: 2 };
console.log(updated);

Output:

{ x: 1, y: 2 }

Learn it in a lesson

See also

Back to the Dictionary