Cookies improve the way our website works, by using this website you are agreeing to our use of cookies. For more information see our privacy policy.
OK
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];
}
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.
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!
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