curriculum/challenges/english/blocks/lecture-working-with-the-arguments-object-and-rest-parameters/68bcc3b6a7480288cd981be0.md
As you recall from earlier lessons and workshops, you can create functions with a number of parameters and call that function with arguments.
Here is a reminder:
// function definition
function getSum(num1, num2) {
return num1 + num2;
}
// function call
getSum(3, 4); // 7
But what if you have a function that is called with more arguments than it was defined to accept?
:::interactive_editor
// function definition
function getSum(num1, num2) {
return num1 + num2;
}
// function call with extra argument
console.log(getSum(3, 4, 5)); // 7
:::
JavaScript will not throw an error in this case. It will instead ignore the extra argument and just add the numbers 3 and 4 together. Functions that accept a variable number of arguments are known as <dfn>variadic functions</dfn>.
If you are working with variadic functions, then you can utilize the arguments object. This array-like object contains the values of the arguments passed into the function.
Here is an example:
:::interactive_editor
function logArgs() {
for (const arg of arguments) {
console.log(arg);
}
}
logArgs(1, 2, 3);
// result:
// 1
// 2
// 3
logArgs("example"); // "example"
:::
Since the arguments object is array-like, you can access an argument at a specific index like this:
:::interactive_editor
function getArg() {
return arguments[1];
}
console.log(getArg(2, 4, 6)); // 4
:::
You can also use the length property like this to get the number of arguments the function was called with:
:::interactive_editor
function getArgs() {
return arguments.length;
}
console.log(getArgs("Example")); // 1
console.log(getArgs("Another", "Example")); // 2
:::
Even though the arguments object appears to act like a real array, it does not have built in Array methods like includes or push. To have access to those methods, you would need to first convert the arguments object to a real array using something like slice, Array.from() or the spread operator:
:::interactive_editor
function hasCat() {
return [...arguments].includes("cat");
}
console.log(hasCat("dog", "chicken", "cat")); // true
console.log(hasCat("dog", "chicken", "horse")); // false
:::
While it is possible to work with the arguments object for variadic functions, modern JavaScript applications will normally use rest parameter syntax. You will learn more about that in a future lesson.
What is a variadic function?
Functions that accept two arguments.
Refer to the beginning of the lesson for the answer.
Functions that accept a variable number of arguments.
Functions that accept a single argument.
Refer to the beginning of the lesson for the answer.
Functions that accept more than three arguments.
Refer to the beginning of the lesson for the answer.
2
What is the arguments object?
A special dictionary that contains the values of the arguments passed into a function.
Refer to the beginning of the lesson for the answer.
An array-like object that contains the values of the arguments passed into a function.
A variable that contains only the first argument passed into a function.
Refer to the beginning of the lesson for the answer.
A true array that automatically updates when function parameters are reassigned.
Refer to the beginning of the lesson for the answer.
2
Why can't you use built in Array methods like includes or push on the arguments object?
The arguments object is always empty unless explicitly initialized.
Refer to the end of the lesson for the answer.
JavaScript automatically blocks method calls on the arguments object for performance reasons.
Refer to the end of the lesson for the answer.
The includes or push methods unreliably work on the arguments object so those methods should be avoided.
Refer to the end of the lesson for the answer.
The arguments object is not a real array so it doesn't have those built in methods.
4