Skip to main content

The Spread Operator

In JavaScript, the spread operator (...) is a convenient way to copy the contents of one array or object into another. It does not modify the original value. Instead, it creates a new one with the copied contents.

The spread operator works in two places:

Arrays

When used inside an array literal, it expands the elements of an existing array into the new one.

const original = [1, 2, 3];
const updated = [...original, 4]; // [1, 2, 3, 4]

Objects

When used inside an object literal, it copies the enumerable properties from one object into another.

const original = { a: 1, b: 2 };
const updated = { ...original, b: 3 }; // { a: 1, b: 3 }

Why It Matters

The spread operator is an easy way to create new arrays or objects without changing the originals. This is especially useful when working in systems that rely on immutability, where values must not be modified in place. Spread helps you update data while keeping previous versions intact.