What is the difference between the spread and rest operators?
Quick Answer
Both use .... Spread expands an iterable into individual elements — [...a, ...b], fn(...args), {...obj, extra: 1}. Rest collects the remaining items into one array/object — function fn(first, ...others), const [head, ...tail] = arr.
Same three dots, opposite jobs. Spread takes something iterable and spreads it out — combining arrays with [...a, ...b], cloning an object with {...obj}, passing an array as function arguments. Rest gathers up whatever's left into one array — a function that takes (first, ...rest), or destructuring with const [head, ...tail]. The one thing to remember is that spread only does a shallow copy, so nested objects are still shared.
- Spread makes a shallow copy — nested objects are still shared references.
{...defaults, ...overrides}is the idiomatic merge (later keys win).- Rest in destructuring:
const { id, ...rest } = propsto split off known keys. - Rest parameters replace the old
argumentsobject and are real arrays.