Files
freeCodeCamp/curriculum/challenges/english/blocks/lecture-working-with-the-arguments-object-and-rest-parameters/68bcc3b6a7480288cd981be0.md
T

4.3 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68bcc3b6a7480288cd981be0 What is the arguments Object? 19 what-is-the-arguments-object

--description--

As you recall from earlier lectures 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?

// 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 variadic functions.

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:

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:

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:

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:

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 lecture.

--questions--

--text--

What is a variadic function?

--answers--

Functions that accept two arguments.

--feedback--

Refer to the beginning of the lecture for the answer.


Functions that accept a variable number of arguments.


Functions that accept a single argument.

--feedback--

Refer to the beginning of the lecture for the answer.


Functions that accept more than three arguments.

--feedback--

Refer to the beginning of the lecture for the answer.

--video-solution--

2

--text--

What is the arguments object?

--answers--

A special dictionary that contains the values of the arguments passed into a function.

--feedback--

Refer to the beginning of the lecture 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.

--feedback--

Refer to the beginning of the lecture for the answer.


A true array that automatically updates when function parameters are reassigned.

--feedback--

Refer to the beginning of the lecture for the answer.

--video-solution--

2

--text--

Why can't you use built in Array methods like includes or push on the arguments object?

--answers--

The arguments object is always empty unless explicitly initialized.

--feedback--

Refer to the end of the lecture for the answer.


JavaScript automatically blocks method calls on the arguments object for performance reasons.

--feedback--

Refer to the end of the lecture for the answer.


The includes or push methods unreliably work on the arguments object so those methods should be avoided.

--feedback--

Refer to the end of the lecture for the answer.


The arguments object is not a real array so it doesn't have those built in methods.

--video-solution--

4