JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)
What are the differences between arguments object and rest parameter
There are three main differences between arguments object and rest parameters
- The arguments object is an array-like but not an array. Whereas the rest parameters are array instances.
- The arguments object does not support methods such as sort, map, forEach, or pop. Whereas these methods can be used in rest parameters.
- The rest parameters are only the ones that haven’t been given a separate name, while the arguments object contains all arguments passed to the function
Practical Code Example
javascript
// Arguments Object vs Rest Parameters:
// 1. arguments: Array-like object (no map/filter), available in traditional functions only
function oldWay() {
console.log(arguments.length);
// arguments.map(...) // TypeError: arguments.map is not a function
}
// 2. rest parameter: Real JavaScript Array with full array methods, works in arrow functions:
function modernWay(...args) {
return args.map(x => x * 2);
}