AceDevHub
Beginner JavaScript Interview QuestionsBeginnerComparison

JavaScript · Question 20

What is the difference between spread syntax and rest syntax in JavaScript?

Direct answer

Both use ..., but spread expands values while rest collects multiple values into a single array or object pattern.

Spread appears where values are being expanded, for example const copy = [...items] or fn(...args). Rest appears where remaining values are being collected, for example function fn(...args) {}.

  • Array spread expands iterable elements into a new array or function call.
  • Object spread copies enumerable own properties into a new object.
  • Rest parameters collect remaining function arguments into an actual array.
  • Rest patterns can collect remaining properties or elements during destructuring.