Learn JavaScript
❯ objects.spread_rest

Spread & Rest (...syntax) in JavaScript

$concept

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.

$how it works
1

Spread expands

[...a, ...b] concatenates; f(...args) calls with array elements as arguments; {...o} clones objects shallowly.

2

Rest gathers

function f(first, ...rest) and const [h, ...t] = list collect remainders into true arrays.

3

Later keys win

{...defaults, ...user} merges with overrides prevailing β€” the options-pattern one-liner.

4

Shallow everywhere

Nested objects spread by reference β€” deep independence needs structuredClone or manual recursion.

$example
let base = { a: 1, b: 2 };
let merged = { ...base, b: 3, c: 4 };
console.log(merged.a, merged.b, merged.c);
Output
1 3 4
1Spread copies base's fields into the fresh literal.
2Later b: 3 overrides the spread b: 2; c: 4 adds new.
31 3 4 prints β€” merge-with-overrides in one expression.
$use cases
Immutable updates ({...state, field: value}) in React/Redux
Merging defaults with user options
Variadic functions via rest parameters
Cloning arrays/objects (shallowly) without manual loops

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself β€” building beats watching.