What does the spread operator (...) do when used on an array?
Short answer
Spread expands an iterable's elements in place, commonly used to copy an array or merge multiple arrays into a new one without mutating the originals.
Advertisement
AModel answer
Spread expands an iterable's elements in place, commonly used to copy an array or merge multiple arrays into a new one without mutating the originals.
const a = [1, 2];
const b = [3, 4];
const combined = [...a, ...b]; // [1, 2, 3, 4]Real-world example
Merging a default list of settings with user-provided overrides.
Likely follow-ups
- Does spreading an array create a deep copy or a shallow copy?
Related topics