Spread & Rest (...syntax) in JavaScript
Three dots do three jobs by position: spread expands iterables into calls/arrays ([...a, ...b], f(...args)) and objects into literals ({...defaults, ...overrides}); rest collects leftovers into true arrays (function (...args), const [first, ...rest]). Same glyph, opposite directions β expanding versus gathering.
Object spread copies own-enumerable properties shallowly (nested objects stay shared) and later keys win ({...a, x: 1} overrides a.x). Rest must trail (parameters, patterns) β one rest per list, always last.
Spread expands
[...a, ...b] concatenates; f(...args) calls with array elements as arguments; {...o} clones objects shallowly.
Rest gathers
function f(first, ...rest) and const [h, ...t] = list collect remainders into true arrays.
Later keys win
{...defaults, ...user} merges with overrides prevailing β the options-pattern one-liner.
Shallow everywhere
Nested objects spread by reference β deep independence needs structuredClone or manual recursion.
let base = { a: 1, b: 2 };let merged = { ...base, b: 3, c: 4 };console.log(merged.a, merged.b, merged.c);Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.