Sub topic 9
The arguments Object and Why Arrows Lack It
This chapter assumes you can write function declarations and arrow functions, and that you know what rest parameters are from the previous chapters. It covers the older arguments object, the reason arrow functions have no arguments of their own, and the replacement you should use in new code.
What arguments Is
Every regular function, whether a declaration or an expression, gets a local arguments object when it is called. The object holds every value the caller passed, including values that have no matching named parameter.
function regular() {
return arguments.length;
}
const arrow = (...args) => args.length;
console.log(regular(1, 2, 3)); // 3
console.log(arrow(1, 2, 3)); // 3The regular function declares no parameters, yet it can still count three arguments. The arrow function gets the same result through a rest parameter.
Array-Like, Not an Array
arguments has numeric indexes and a length property, but it is not an array. It has none of the array methods, so code that calls .map or .reduce on it throws a TypeError.
function sumAll() {
console.log(Array.isArray(arguments)); // false
console.log(typeof arguments.reduce); // "undefined"
return Array.from(arguments).reduce((total, n) => total + n, 0);
}
console.log(sumAll(1, 2, 3)); // 6Before ES2015, developers converted it with Array.prototype.slice.call(arguments). Array.from(arguments) or [...arguments] does the same job today, but a rest parameter avoids the conversion entirely.
Why Arrows Lack It
Arrow functions do not create their own this, and they do not create their own arguments either. Inside an arrow, the name arguments is resolved like any other variable: JavaScript looks outward through the enclosing scopes until it finds one.
function outer() {
const inner = () => arguments[0];
return inner('ignored');
}
console.log(outer('from outer')); // from outerThe arrow inner was called with 'ignored', but arguments[0] read the value passed to outer. This is the same lexical behavior as this, and it is useful when you need it, such as an arrow callback inside a regular function that forwards the outer function's arguments. In most cases, though, it is a source of bugs.
Rest Parameters Replace It
A rest parameter gives you a real array, works in every function form, and makes the function signature say that it accepts extra values. Note one difference: arguments always holds every argument, while a rest parameter holds only the values left after the named parameters.
function logAll(first, ...rest) {
console.log(arguments.length); // 3
console.log(rest.length); // 2
console.log(rest); // [ 2, 3 ]
}
logAll(1, 2, 3);| Feature | `arguments` | Rest parameter |
|---|---|---|
| Available in arrows | No (lexical lookup) | Yes |
| Real array | No | Yes |
| Includes named parameters | Yes | No, only the remaining values |
| Visible in the signature | No | Yes |
Common Errors
- Calling array methods directly on
arguments, which throwsTypeError: arguments.map is not a function. - Converting a regular function to an arrow without noticing that its body uses
arguments. The code then reads an outer function's values or throws. - Expecting a rest parameter to include the named parameters, which leads to off-by-one bugs.
Exercise
What does this code print, and why? function wrap(a) { const show = (b) => arguments.length + ':' + b; return show(10); } console.log(wrap(1, 2, 3));
Show answer
It prints "3:10". The arrow show has no arguments object of its own, so arguments refers to the one belonging to wrap, which received three values. The parameter b is still the arrow's own parameter, so it is 10.
Exercise
Rewrite function max() { return Math.max.apply(null, arguments); } as an arrow function that works the same way.
Show answer
const max = (...nums) => Math.max(...nums); The rest parameter collects every argument into a real array, and spread syntax passes them on to Math.max, so you no longer need apply.