arguments Array

By: Dusty Arlia
Published on Tuesday, May 21, 2013, 10:01 PM
Last Updated on Friday, July 03, 2015 at 6:55 PM
Total Updates: 5

The arguments that get passed to a function automatically get stored into an array-like object called arguments. This object is a local variable to the function. The arguments array has an element for each argument passed to the function. The first argument gets assigned to index 0.

Reference the first argument using this syntax:


The arguments array is a second location where your function's arguments get stored. The primary place to access your function's arguments is the designated parameters defined in the function declaration. It's common practice that you should specify enough parameters in your function declaration to receive the amount of arguments you plan on passing. This makes your code easier to read and maintain. If your function receives an arbitrary number of arguments, pass an array as a single argument instead.

The arguments array can also hold an arbitrary number of arguments. Here is an example:

function number_adder() {
    var arguments_array_length = arguments.length;
    var total = 0;
    for (var i = 0; i < arguments_array_length; i++){
        total = total + arguments[i];
    return total;

var number_adder_results = number_adder(1,2,3);

You are able to include any number of arguments you want in the function call above, including no arguments at all. In this example the function call is number_adder(1,2,3), where the numbers 1, 2, and 3 are the arguments.

When you initialize total to 0, total will always have a number value. This example outputs an alert box with the number 6 in it. Here are the steps that lead to that:

  1. The number_adder() function is declared and initialized.
  2. In its function body, the variable arguments_array_length is declared and initialized to the length of the arguments array.
  3. Next, the total variable is declared and initialized to 0.
  4. Next, a for loop is defined.
  5. The condition (i) is initialized to 0.
  6. The condition makes sure i is less than the length of the arguments array (the arguments_array_length variable).
  7. i is incremented (increments after the first time through).
  8. The total variable gets the value of each arguments array element added to it.
  9. The return statement will return the value of the total variable to the function call when this function runs.
  10. Variable number_adder_results gets assigned the results of the number_adder() function. In this example, the function call passes the arguments 1, 2, and 3 to the number_adder() function. Since there is no parameters, these values get stored in the arguments array.
  11. The number_adder_results variable gets displayed in an alert box.

On line 5, in the example above, the code looks like this:

total = total + arguments[i];

This line could be written to look like this:

total += arguments[i];

The += symbol is called the addition assignment operator. It makes the code for adding a number to a variable more streamline and concise (but less explicit). It is one of a couple different shortcut mathematical operators.