Parameter and arguments aliasing
suggest changeTraditional, non-arrow functions receive an array-like arguments object. In a sloppy-mode function with a simple parameter list, indexed arguments and named parameters can refer to the same values:
function update(value) {
value = 5;
console.log(arguments[0]); // 5 in sloppy mode
}
update(1);
Strict mode removes that aliasing:
function update(value) {
"use strict";
value = 5;
console.log(arguments[0]); // 1
}
update(1);
A non-simple parameter list—one containing a default, rest parameter, or destructuring pattern—also uses an unmapped arguments object, even without a "use strict" directive:
function update(value = 0) {
value = 5;
console.log(arguments[0]); // 1
}
update(1);
Prefer rest parameters in new code. They produce a real array, clearly express which values are collected, and avoid the legacy aliasing behavior:
function sum(...values) {
return values.reduce((total, value) => total + value, 0);
}
Arrow functions do not define their own arguments; use rest parameters with them.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents