Functions and the arguments object
JavaScript deals with arguments in functions by adding them to a custom object called arguments
. This object works a lot like an array, and we can use it instead of using the name of the parameter. Consider the following code:
function test(a, b, c) {
console.log("first:", a, arguments[0]);
console.log("second:", b, arguments[1]);
console.log("third:", c, arguments[2]);
}
test("fun", "js", "secrets");
This outputs:
first: fun fun
second: js js
third: secrets secrets
When you update one of the parameters, the argument gets changed accordingly. The same goes for the other way around;
function test(a, b, c) {
a = "nice";
arguments[1] = "JavaScript";
console.log("first:", a, arguments[0]);
console.log("second:", b, arguments[1]);
console.log("third:", c, arguments[2]);
}
test("fun", "js"...