Skip to content
IT glossary
FrontendUpdated December 3, 2026

Spread and rest syntax

Spread и rest

JavaScript uses the same three dots (`...`) for two jobs. Spread expands values or properties into a new context; rest collects whatever is left into an array or object.

One notation, two jobs

In JavaScript, ... can either unpack a source or gather what is left over. Read send(...values) as “pass these values as separate arguments.” Read function send(...values) {} as “collect the arguments passed to this function in an array.” The first is spread at the call site; the second is rest in the parameter list.

Where spread works

In an array literal, [...items, 'done'] builds a new array with the contents of items followed by one extra element. In a function call, Math.max(...[4, 9]) is equivalent to Math.max(4, 9). Both forms require an iterable, such as an array, a string, or a Set. A plain object won't work in either form unless it implements iteration.

Object spread follows a different rule. { ...defaults, ...overrides } copies each source's own enumerable properties, not inherited properties. If both sources have the same key, the later value wins. This makes the order of the spreads important when applying configuration overrides or updating fields in UI state.

Where rest works

A rest parameter such as ...values in function send(topic, ...values) {} puts every argument after topic into a real array. With no extra arguments, that array is empty. Rest also works in destructuring: const [head, ...tail] = items collects the remaining values in an array, while const { id, ...fields } = item collects the remaining own enumerable properties in a new object. Rest must come last in the parameter list or destructuring pattern.

Example: updating a nested field

const state = { theme: 'light', user: { name: 'Avery', active: false } };
const next = {
  ...state,
  user: { ...state.user, active: true },
};

The outer spread preserves theme. The user property that follows replaces the original user reference with a new object. The inner spread preserves name, and active: true overrides the old value. This is a common pattern in React: copy the objects along the path to the field you're changing rather than mutating the previous state.

What interviewers check

Expect a question about shallow copies. If you write const copy = { ...state } and then set copy.user.active = true, the original state's nested field changes too: copy and state are different objects, but copy.user === state.user. Array spread behaves the same way for elements that are objects. A new container does not imply new copies of the values it contains.