EasyFunctions📖 Theory Question

What are rest parameters and how do they differ from the arguments object?

💡

Hint

Rest (...args) is a real Array; arguments is array-like, no arrow support, no Array methods

Full Answer

Rest parameters (...args) collect remaining function arguments into a real Array.

// Rest parameters — modern
function sum(first, ...rest) {
  console.log(first);   // 1
  console.log(rest);    // [2, 3, 4] — real Array!
  return rest.reduce((a, b) => a + b, first);
}
sum(1, 2, 3, 4); // 10
rest.map(x => x * 2); // ✅ has Array methods

// arguments — legacy
function old() {
  console.log(arguments);        // { 0:1, 1:2, ... } — array-LIKE
  console.log(arguments.map);    // undefined — NOT a real Array
  const arr = Array.from(arguments); // convert needed
}

// Arrow functions have NO arguments object
const arrow = () => {
  console.log(arguments); // ReferenceError!
  // Use rest: (...args) => { console.log(args) }
};

Key differences:

  • Rest is a real Array → has all array methods
  • arguments is array-like → no map/filter/etc
  • Arrow functions don't have arguments
  • Rest collects only the remaining args after named params
💡 Always use rest parameters in modern code. arguments is legacy and has quirks that trip people up.

More Functions Questions

EasyWhat is the difference between call, apply, and bind?→EasyHow do arrow functions differ from regular functions?→EasyWhat is a pure function and why does it matter?→EasyWhat are Higher-Order Functions (HOF)?→

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint