Rest Parameters

Easy

Rest parameters collect remaining arguments into an array. Cleaner than the arguments object and works with arrow functions.

Interactive Visualization

Destructuring

Old Way
const user = { name: "Alice", age: 25 };
const name = user.name;
const age = user.age;
Modern Way
Click transform to see

Key Points

  • function(...args)
  • Must be last parameter
  • Real array (unlike arguments)
  • Works with arrow functions

Code Examples

Rest vs Arguments

function sum(...numbers) {
  return numbers.reduce((a, b) => a + b, 0);
}
sum(1, 2, 3); // 6

// Rest is real array
const logAll = (...args) => args.forEach(console.log);

Rest collects args into a real array with all methods