Arguments Object In JavaScript

Arguments object in JavaScript allows us to access the argument values inside the function. In this tutorial, we will show you what is Arguments object and how to access it inside the JavaScript function. We also look at some of its use cases with examples.

What is Arguments Object

The Arguments object is an array-like object available inside every JavaScript function. It contains the values of the arguments passed to that function.

We generally use the parameters to access the arguments inside the function. But JavaScript allows us to pass any number of arguments to a function irrespective of the number of parameters it declares

The following addNum function declares two parameters. But we can invoke it with three or more arguments. JavaScript does not complain. But we cannot access those additional arguments using the existing parameters. This is where we use the Arguments object.

How to Access Arguments Object

The Arguments object is always available inside every function (except arrow functions). It contains the values of the arguments in an Array-like object with the first entry’s index at 0. Note that it is not an Array but an Array-like object.

The value of the first argument is available at the 0th index i.e. arguments[0]. The next one will be at arguments[1] and so on.

For Example, take a look at the following example. The addNumbers declares three parameters. We can access all of them using the Arguments object.

The Arguments object returns undefined if we try to access the non-existing argument.

You can also refer to arguments as addNumbers.arguments[0]. But this feature is deprecated. So it is no longer recommended. Though some browsers still support it for compatibility purposes

Properties of Argument Object

The Argument object has two properties.

Length Property returns the total number of arguments that were passed to the function.

Callee Property returns the reference to the executing function to which the arguments belong. This property is not available in strict mode.

Arguments Object Examples

The following example shows how we can make use of the arguments object. The following function addNumbers can add any number of numbers. The code uses the arguments.length property to find out the number of arguments. It uses it in a for loop and calculates the sum of all arguments.

The following code finds the largest number.

Using callee to recursively call the function.

Modifying the Values of Arguments Object

We can reassign new values to the arguments object. In Non-strict mode, the Parameters are also automatically synchronized with the new value.

In the following code, we assign a new value to arguments[0]. This will also change the value of the parameter a.

But in strict mode Parameters will not change.

vice versa is also true. Any modifications to Parameters will automatically change the arguments object.

But it will not work in strict Mode

The above work only with the simple parameter. It will not work with the rest parameters, default parameters, or destructured parameters.

In the following code, we change the arguments[0]. This will also change the message variable.

But when we assign a default value to message parameter, any changes made to arguments[0] does not change the message parameter.

Even updating the message parameter does not change the arguments[0]

When we do not pass any argument value, then the argument[0] remains undefined

Converting into array

The arguments object looks like an array but it is not an array. It has only two properties length and callee. It does not have methods like forEach, reduce, filter and map, etc.

But you can convert it into an array using

The arguments object is not available in Arrow functions

But that is not a big deal as you can make use of rest parameters (...arguments).



Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top