Rest vs Arguments Object
You'll learn how rest parameters provide a cleaner, more powerful alternative to the legacy arguments object.
The Old Way: arguments
Before rest parameters existed, JavaScript provided an arguments object inside every function. This object looked like an array and contained all the arguments passed to the function, but it wasn't a true array—it was an "array-like" object.
function oldWay() {
console.log(arguments); // array-like object
console.log(Array.isArray(arguments)); // false
// arguments.map(...) won't work!
}
oldWay(1, 2, 3);
This caused problems because you couldn't use array methods like .map(), .filter(), or .reduce() directly on arguments.
The Modern Way: Rest Parameters
Rest parameters (...args) give you a real array containing all the extra arguments:
function modernWay(...numbers) {
console.log(numbers); // true array
console.log(Array.isArray(numbers)); // true
return numbers.map(n => n * 2); // works perfectly!
}
modernWay(1, 2, 3); // [2, 4, 6]
Key Differences
Rest parameters are better because:
- They're true arrays with all array methods available
- They only capture "remaining" arguments (you can have named parameters before them)
- They're more readable and explicit about intent
- They work in arrow functions (which don't have
arguments)
The arguments object:
- Is array-like but not a true array
- Always captures all arguments
- Doesn't work in arrow functions
- Considered legacy syntax
Key Takeaway: Rest parameters replace the arguments object with true arrays, giving you access to all array methods and clearer, more modern code.