You are here: Reference > JavaScript > core > objects > arguments

arguments object

Browser support:
An instance of the arguments object is always available within the currently executing function and it contains information about the parameters specified by the caller and the caller function.
The arguments object is not available directly from JavaScript, only the instances can be used. The arguments object behaves like the Array object, the number of specified parameters can be retrieved with the length property and the parameter at a given position can be accessed via the [] operator.

Syntax:

Getting the arguments object:
function Test (arg1, arg2) {
    var argCount = arguments.length;
    var firstParam = arguments[0];
}
Did you find this example helpful? yes no
The value of the arguments.length property is the count of the parameters specified by the caller, arguments[0] is the first parameter (arg1), if exists. For a complex example, see the examples section below.

Members:

The arguments object inherits from the Object.prototype object. The following lists only contain the members of the arguments object.

Properties:

Property Support Description
callee
Returns the body of the current function.
caller
9
Returns the function that called the current function. The arguments.caller property is deprecated in JavaScript 1.3 and it has been removed in Internet Explorer 9. It is no longer recommended to use!
length
Returns the number of arguments specified by the caller.

Methods:

Method Support Description
[] (index)
Returns an argument at the specified position.
index Required. Integer that specifies the position of the argument to get.

Examples:

Example 1:

Parameter test.
function TestArguments (param1, param2) {
    var paramCount = TestArguments.length;
    var argCount = arguments.length;

    document.write ("Number of declared parameters: " + paramCount);
    document.write ("<br />");
    document.write ("Number of parameters specified by the caller: " + argCount);
    document.write ("<br />");

    for (var i = 0; i < argCount; i++) {
        document.write ("The value of arguments[" + i + "] is " + arguments[i]);
        document.write ("<br />");
    }

    document.write ("The value of param1: " + param1);
    document.write ("<br />");
    document.write ("The value of param2: " + param2);
}
    

document.write ("call TestArguments ('first')");
document.write ("<br />");
TestArguments ('first');

document.write ("<br /><br />");

document.write ("call TestArguments ('first', 'second')");
document.write ("<br />");
TestArguments ('first', 'second');

document.write ("<br /><br />");

document.write ("call TestArguments ('first', 'second', 'third')");
document.write ("<br />");
TestArguments ('first', 'second', 'third');
Did you find this example helpful? yes no

Example 2:

This example shows how to implement a function that receives a variable-length argument list and returns their sum.
function Add () {
    var sum = 0;
    for (var i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }
    return sum;
}

var n = Add (2, 3);
document.write (n);  // 5

document.write ("<br />");

n = Add (1, 2, 3, 4, 5);
document.write (n);  // 15
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content