← Back to JavaScript, page 1

javascript-0009 · Easy fresher

What is the spread operator, and where is it used?

The spread operator ... expands an iterable or an object into individual elements or properties. It is used to copy arrays and objects, merge them, pass array items as function arguments, and build updated copies without changing the original. The copies it makes are shallow.

Full answer

Spread takes something that holds many values and lays them out one by one at the place where it is written. It has three common homes.

In an array literal, [...a, ...b] builds a new array with the items of both. In a function call, Math.max(...marks) passes each item as a separate argument. In an object literal, { ...base, ...changes } copies the own enumerable properties of each object, and when two objects have the same key, the later one wins.

Spread is the standard tool for updates that must not change the original, such as state in a UI library: { ...user, city: 'Delhi' } makes a new object with one value replaced.

Two details cause trouble. First, the copy is shallow. Nested objects are shared between the original and the copy, so changing a nested value changes both. Second, object spread copies only own enumerable properties, so it skips inherited ones and does not copy getters as getters; it reads their current values. Array spread works on any iterable, including strings, Sets, and Maps, but object spread works on objects only for properties, not for iteration.

When the same three dots appear in a function parameter list or on the left side of a destructuring pattern, they mean rest and gather values instead.

javascript
const morning = ['maths', 'physics'];
const evening = ['chemistry'];
const all = [...morning, ...evening];
console.log(all);

console.log(Math.max(...[72, 91, 64]));
console.log([...'abc']);
console.log([...new Set([1, 2, 2, 3])]);

const defaults = { theme: 'light', fontSize: 14 };
const saved = { theme: 'dark' };
console.log({ ...defaults, ...saved });

const user = { name: 'Neha', address: { city: 'Pune' } };
const copy = { ...user };
copy.name = 'Changed';
copy.address.city = 'Delhi'; // nested object is shared
console.log(user.name, user.address.city);

const updated = { ...user, name: 'Neha S.' };
console.log(updated === user, updated.address === user.address);

Run with Node.js v24.22.1 as js-9.js; two runs gave identical output. Output: [ 'maths', 'physics', 'chemistry' ] 91 [ 'a', 'b', 'c' ] [ 1, 2, 3 ] { theme: 'dark', fontSize: 14 } Neha Delhi false true

Key points

  • Spread expands arrays, iterables, and object properties in place.
  • Later properties override earlier ones with the same key.
  • Copies are shallow; nested objects stay shared.
  • Works in array literals, function calls, and object literals.
  • The same ... on the receiving side is the rest syntax.

Common mistakes

  • Treating spread as a deep copy and then mutating nested data.
  • Spreading a non-iterable such as a plain object into an array literal, which throws a TypeError.
  • Copying large arrays with spread inside a loop and creating a quadratic cost.
  • Passing a very large array to a function with spread; the engine can run out of stack space for arguments.

Follow-ups

  • How would you make a deep copy instead?
  • What happens when you spread null or undefined into an object?
  • How does the order of keys in a spread affect the final result?

Tips

  • Explain shallow copying with the nested address example; interviewers often ask about it next.
  • Contrast spread (expand) with rest (collect) in one sentence.
  • Mention that spread copies a reference to inner objects, so equality on inner values still holds.