--- title: Arguments object slug: Web/JavaScript/Reference/Functions/arguments tags: - Functions - JavaScript - NeedsTranslation - TopicStub - arguments translation_of: Web/JavaScript/Reference/Functions/arguments ---
The arguments
object is an Array
-like object corresponding to the arguments passed to a function.
arguments
The arguments
object is a local variable available within all functions. arguments
as a property of Function
can no longer be used.
You can refer to a function's arguments within the function by using the arguments
object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0. For example, if a function is passed three arguments, you can refer to the argument as follows:
arguments[0] arguments[1] arguments[2]
The arguments can also be set:
arguments[1] = 'new value';
The arguments
object is not an {{jsxref("Array")}}. It is similar to an Array
, but does not have any Array
properties except length
. For example, it does not have the pop
method. However it can be converted to a real Array
:
var args = Array.prototype.slice.call(arguments);
Important: You should not slice on arguments because it prevents optimizations in JavaScript engines (V8 for example). Instead, try constructing a new array by iterating through the arguments object. More information.
If Array generics are available, one can use the following instead:
var args = Array.slice(arguments);
The arguments
object is available only within a function body. Attempting to access the arguments
object outside a function declaration results in an error.
You can use the arguments
object if you call a function with more arguments than it is formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments. You can use arguments.length
to determine the number of arguments passed to the function, and then process each argument by using the arguments
object. (To determine the number of arguments declared when a function was defined, use the Function.length
property.)
arguments.callee
arguments.caller
{{ Obsolete_inline() }}arguments.length
This example defines a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:
function myConcat(separator) { var args = Array.prototype.slice.call(arguments, 1); return args.join(separator); }
You can pass any number of arguments to this function, and it creates a list using each argument as an item in the list.
// returns "red, orange, blue" myConcat(", ", "red", "orange", "blue"); // returns "elephant; giraffe; lion; cheetah" myConcat("; ", "elephant", "giraffe", "lion", "cheetah"); // returns "sage. basil. oregano. pepper. parsley" myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");
This example defines a function that creates a string containing HTML for a list. The only formal argument for the function is a string that is "u
" if the list is to be unordered (bulleted), or "o
" if the list is to be ordered (numbered). The function is defined as follows:
function list(type) { var result = "<" + type + "l><li>"; var args = Array.prototype.slice.call(arguments, 1); result += args.join("</li><li>"); result += "</li></" + type + "l>"; // end list return result; }
You can pass any number of arguments to this function, and it adds each argument as an item to a list of the type indicated. For example:
var listHTML = list("u", "One", "Two", "Three"); /* listHTML is: "<ul><li>One</li><li>Two</li><li>Three</li></ul>" */
The arguments
object can be used in conjunction with rest parameters, default parameters or destructured parameters.
function foo(...args) { return arguments; } foo(1, 2, 3); // { "0": 1, "1": 2, "2": 3 }
However, in non-strict functions, a mapped arguments
object is only provided if the function does not contain any rest parameters, any default parameters or any destructured parameters. For example, in the following function that uses a default parameter, 1
0 instead of 100 is returned:
function bar(a=1) { arguments[0] = 100; return a; } bar(10); // 10
Specification | Status | Comment |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. Implemented in JavaScript 1.1 |
{{SpecName('ES5.1', '#sec-10.6', 'Arguments Object')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-arguments-exotic-objects', 'Arguments Exotic Objects')}} | {{Spec2('ES6')}} |
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |