The arguments object
BaselineWidely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
arguments
is an array-like object accessible insidefunctions that contains the values of the arguments passed to that function.
Try it
function func1(a, b, c) { console.log(arguments[0]); // Expected output: 1 console.log(arguments[1]); // Expected output: 2 console.log(arguments[2]); // Expected output: 3}func1(1, 2, 3);
Description
Note:In modern code,rest parameters should be preferred.
Thearguments
object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using itsarguments
object. It has entries for each argument the function was called with, with the first entry's index at0
.
For example, if a function is passed 3 arguments, you can access them as follows:
arguments[0]; // first argumentarguments[1]; // second argumentarguments[2]; // third argument
Thearguments
object is useful for functions called with more arguments than they are formally declared to accept, calledvariadic functions, such asMath.min()
. This example function accepts any number of string arguments and returns the longest one:
function longestString() { let longest = ""; if (arguments.length === 0) { throw new TypeError("At least one string is required"); } for (const arg of arguments) { if (arg.length > longest.length) { longest = arg; } } return longest;}
You can usearguments.length
to count how many arguments the function was called with. If you instead want to count how many parameters a function is declared to accept, inspect that function'slength
property.
Assigning to indices
Each argument index can also be set or reassigned:
arguments[1] = "new value";
Non-strict functions that only have simple parameters (that is, no rest, default, or destructured parameters) will sync the new value of parameters with thearguments
object, and vice versa:
function func(a) { arguments[0] = 99; // updating arguments[0] also updates a console.log(a);}func(10); // 99function func2(a) { a = 99; // updating a also updates arguments[0] console.log(arguments[0]);}func2(10); // 99
Non-strict functions thatare passedrest,default, ordestructured parameters will not sync new values assigned to parameters in the function body with thearguments
object. Instead, thearguments
object in non-strict functions with complex parameters will always reflect the values passed to the function when the function was called.
function funcWithDefault(a = 55) { arguments[0] = 99; // updating arguments[0] does not also update a console.log(a);}funcWithDefault(10); // 10function funcWithDefault2(a = 55) { a = 99; // updating a does not also update arguments[0] console.log(arguments[0]);}funcWithDefault2(10); // 10// An untracked default parameterfunction funcWithDefault3(a = 55) { console.log(arguments[0]); console.log(arguments.length);}funcWithDefault3(); // undefined; 0
This is the same behavior exhibited by allstrict-mode functions, regardless of the type of parameters they are passed. That is, assigning new values to parameters in the body of the function never affects thearguments
object, nor will assigning new values to thearguments
indices affect the value of parameters, even when the function only has simple parameters.
Note:You cannot write a"use strict";
directive in the body of a function definition that accepts rest, default, or destructured parameters. Doing so will throwa syntax error.
arguments is an array-like object
arguments
is an array-like object, which means thatarguments
has alength
property and properties indexed from zero, but it doesn't haveArray
's built-in methods likeforEach()
ormap()
. However, it can be converted to a realArray
, using one ofslice()
,Array.from()
, orspread syntax.
const args = Array.prototype.slice.call(arguments);// orconst args = Array.from(arguments);// orconst args = [...arguments];
For common use cases, using it as an array-like object is sufficient, since it bothis iterable and haslength
and number indices. For example,Function.prototype.apply()
accepts array-like objects.
function midpoint() { return ( (Math.min.apply(null, arguments) + Math.max.apply(null, arguments)) / 2 );}console.log(midpoint(3, 1, 4, 1, 5)); // 3
Properties
arguments.callee
DeprecatedReference to the currently executing function that the arguments belong to. Forbidden in strict mode.
arguments.length
The number of arguments that were passed to the function.
arguments[Symbol.iterator]()
Returns a newarray iterator object that contains the values for each index in
arguments
.
Examples
Defining a function that concatenates several strings
This example defines a function that concatenates several strings. The function's only formal argument is a string containing the characters that separate the items to concatenate.
function myConcat(separator) { const args = Array.prototype.slice.call(arguments, 1); return args.join(separator);}
You can pass as many arguments as you like to this function. It returns a string list using each argument in the list:
myConcat(", ", "red", "orange", "blue");// "red, orange, blue"myConcat("; ", "elephant", "giraffe", "lion", "cheetah");// "elephant; giraffe; lion; cheetah"myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");// "sage. basil. oregano. pepper. parsley"
Defining a function that creates HTML lists
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 beunordered (bulleted), or"o"
if the list is to beordered (numbered). The function is defined as follows:
function list(type) { let html = `<${type}l><li>`; const args = Array.prototype.slice.call(arguments, 1); html += args.join("</li><li>"); html += `</li></${type}l>`; // end list return html;}
You can pass any number of arguments to this function, and it adds each argument as a list item to a list of the type indicated. For example:
list("u", "One", "Two", "Three");// "<ul><li>One</li><li>Two</li><li>Three</li></ul>"
Using typeof with arguments
Thetypeof
operator returns'object'
when used witharguments
console.log(typeof arguments); // 'object'
The type of individual arguments can be determined by indexingarguments
:
console.log(typeof arguments[0]); // returns the type of the first argument
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-arguments-exotic-objects |