arguments
is a special variable passed to all functions so that actual parameters are accessible even when they are not declared as formal parameters.
function a() {
console.log(arguments[0], arguments[1]); // a b
}
a('a', 'b');
In most cases, arguments
is an alias name for formal parameters.
function a(x, y) {
x = 1;
arguments[1] = 2;
console.log(arguments[0], arguments[1], x, y); // 1 2 1 2
}
a('a', 'b');
Here’s how it goes interesting.
a('a', undefined); // 1 2 1 2
a('a'); // 1 2 1 undefined
When an actual parameter is not passed to the function, its behavior is not exactly the same as passing an undefined
. A more profound explanation under the hood should be given once I knew more about JavaScript.