Sub topic 8
The Legacy arguments Object
This chapter assumes you can declare and call functions, work with arrays, and tell parameters apart from arguments. It also assumes you know that JavaScript does not complain when a caller passes more or fewer arguments than a function declares, as the previous chapter showed.
Before rest parameters existed, a function had only one way to read arguments it did not name: the arguments object. Modern code rarely needs it. You will still find it in older libraries, polyfills, and Stack Overflow answers, and it has some odd behavior that can surprise you when you read or maintain that code.
What the arguments Object Holds
Every non-arrow function gets a local binding called arguments when it is called. It holds every value the caller passed, in order. That includes extra arguments that have no parameter name.
function legacySum() {
let total = 0;
for (let i = 0; i < arguments.length; i++) {
total += arguments[i];
}
return total;
}
console.log(legacySum(1, 2, 3)); // 6
console.log(legacySum()); // 0arguments.length is the number of arguments actually passed. It is not the number of parameters declared. This lets you tell a missing argument apart from an argument that was explicitly passed as undefined:
function probe(a, b) {
return arguments.length;
}
console.log(probe()); // 0
console.log(probe(undefined)); // 1
console.log(probe(undefined, undefined)); // 2
console.log(probe(1, 2, 3)); // 3Default values do not show up in arguments. The object records what the caller sent, not what the function ended up using:
function withDefault(a = 5) {
return [a, arguments.length, arguments[0]];
}
console.log(withDefault()); // [5, 0, undefined]Array-Like, Not an Array
arguments has numeric indexes and a length, so it is array-like. Its prototype is Object.prototype, though, not Array.prototype. This means map, filter, reduce, and the other array methods are missing.
function tryMap() {
return typeof arguments.map;
}
console.log(tryMap(1, 2)); // "undefined"
console.log((function () { return Array.isArray(arguments); })()); // falseTo use array methods, convert the object first. Older code used Array.prototype.slice.call(arguments). Modern code uses Array.from:
function doubleAll() {
return Array.from(arguments).map((n) => n * 2);
}
function doubleAllOld() {
return Array.prototype.slice.call(arguments).map((n) => n * 2);
}
console.log(doubleAll(1, 2, 3)); // [2, 4, 6]
console.log(doubleAllOld(1, 2, 3)); // [2, 4, 6]arguments is iterable, so for...of and spread ([...arguments]) also work on it.
Arrow Functions Have No arguments of Their Own
Arrow functions do not create an arguments binding. If you write arguments inside an arrow function, the name resolves to the nearest enclosing non-arrow function, the same way this does.
function outer() {
const inner = () => arguments[0];
return inner("from inner");
}
console.log(outer("from outer")); // "from outer"This is a common source of confusion when someone converts a function expression to an arrow and the code silently starts reading the wrong values. At the top level of an ES module, where no function encloses the arrow, arguments throws a ReferenceError.
The Aliasing Gotcha
In sloppy mode (non-strict code), named parameters and arguments entries can be aliased. Writing to one changes the other. Aliasing applies only when the function has a simple parameter list, meaning plain names with no defaults, rest parameters, or destructuring.
// Run this as a classic script or CommonJS file, not an ES module.
function sloppy(a) {
a = 99;
return arguments[0];
}
function strict(a) {
"use strict";
a = 99;
return arguments[0];
}
function nonSimple(a = 0) {
a = 99;
return arguments[0];
}
console.log(sloppy(1)); // 99, a and arguments[0] are linked
console.log(strict(1)); // 1, strict mode removes the link
console.log(nonSimple(1)); // 1, a default parameter removes the linkThe link exists only for indexes the caller actually passed. Calling sloppy() with no arguments and then assigning a = 99 leaves arguments[0] as undefined.
ES modules and class bodies are always strict, so most modern code never sees aliasing. Older scripts can, and code that writes to parameters and then reads arguments behaves differently depending on the mode.
Other Legacy Features
arguments.calleerefers to the currently running function. Old code used it for recursion in anonymous functions. Accessing it in strict mode throws aTypeError, so use a named function expression instead.- Passing
argumentsto another function (other.apply(null, arguments)) was the old way to forward every argument. Spread syntax, covered in a later chapter, replaces it.
arguments Versus Rest Parameters
The next chapter covers rest parameters in full. This table shows why they replaced arguments in new code:
| Feature | `arguments` | Rest parameter `...args` |
|---|---|---|
| Type | Array-like object | Real `Array` |
| Array methods | Need conversion first | Available directly |
| Available in arrow functions | No (reads the enclosing one) | Yes |
| Contains | Every passed argument | Only arguments after the named ones |
| Visible in the signature | No | Yes |
| Aliasing with parameters | In sloppy mode with simple params | Never |
Use rest parameters in new code. Learn arguments well enough to read old code and to migrate it safely.
Exercise
What does this code log, and why? function f(a, b) { const g = () => arguments.length; return g(1, 2, 3, 4); } console.log(f("x"));
Show answer
It logs 1. The arrow function g has no arguments object of its own, so arguments refers to f's arguments. f was called with one argument ("x"), so its arguments.length is 1. The four values passed to g are not visible through arguments.
Exercise
In a sloppy-mode script, function h(a, b) { b = 10; return arguments[1]; } is called as h(1). What is returned? What changes if the signature becomes function h(a, b = 0)?
Show answer
Both versions return undefined. In the first version the parameter list is simple and the code is sloppy, but aliasing exists only for indexes the caller actually passed. Only index 0 was passed, so assigning b does not create arguments[1]. In the second version, the default makes the parameter list non-simple, which turns off aliasing completely, and arguments[1] is still undefined because nothing was passed there.
Exercise
Rewrite function joinAll() { return Array.prototype.slice.call(arguments).join("-"); } in modern style without using arguments.
Show answer
function joinAll(...parts) { return parts.join("-"); }. A rest parameter collects every argument into a real array, so no conversion is needed. The same version also works as an arrow function: const joinAll = (...parts) => parts.join("-");.