Skip to content

Spread / Rest

In one line

The ... syntax is 'spread' when it expands an iterable into individual elements, and 'rest' when it collects multiple elements into an array.

In simple words

The ... syntax has two roles depending on context. As the spread operator, it expands an iterable (array, string) into individual elements. As the rest parameter, it collects multiple arguments into a single array.

Spread is used in array literals ([...arr1, ...arr2]), object literals ({ ...obj1, ...obj2 }), and function calls (fn(...args)). It creates shallow copies and merges collections.

Rest parameters are used in function declarations to collect remaining arguments: function fn(first, ...rest) puts all arguments after the first into an array called rest. This replaces the old arguments object with a real array.

Example

javascript
// Spread
const a = [1, 2];
const b = [...a, 3]; // [1, 2, 3]

// Rest
function sum(...nums) {
  return nums.reduce((x, y) => x + y, 0);
}
console.log(sum(1, 2, 3)); // 6

Spread expands arrays; rest collects arguments into an array.

Common confusions

  • Confused with: rest

    The difference: Spread expands an iterable into elements (used in calls and literals); rest collects elements into an array (used in function parameters). Same syntax, different role based on context.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary