JavaScript Reference ยท Function
The arguments object
Reference for the JavaScript arguments object: read every value passed to a function, convert it to an array, and why rest parameters are usually better.
Inside a regular function (not an arrow function), arguments is an array-like object that holds every value the caller passed, including extra values that have no matching parameter. It has a length and numeric indexes, so you can loop over it with a classic for loop, but it is not a real array.
Modern code prefers rest parameters, which give you a real array. You still meet arguments in older code and in functions that need to know exactly how many values were passed.
function count() { return arguments.length; } count("a", "b", "c"); // 3
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
arguments
arguments.length
arguments[index]
Value: an object with numeric properties 0, 1, 2 and so on, a length property, and a Symbol.iterator method, so it works with for...of, spread and Array.from(). It has no array methods such as map() or forEach().
Usage notes
- Not available in arrow functions. An arrow function sees the
argumentsof the nearest enclosing regular function, as the second example shows. Use rest parameters in arrow functions. lengthcounts what was passed, not what was declared. The function's ownlengthproperty (describe.length) is the number of declared parameters before the first default or rest parameter.- Not an array.
arguments.mapisundefined. Convert it withArray.from(arguments)or[...arguments], or avoid it with rest parameters. - Sloppy mode aliasing: in non-strict functions with simple parameters, changing
arguments[0]also changes the first parameter and the other way round. In strict mode, and whenever the function uses default, rest or destructured parameters, the link is gone. This is a good reason to use strict mode and modules. - Do not leak it. Passing
argumentsto other functions or slicing it was a known optimization hazard in older engines; copy it with rest parameters instead. - Reassigning is an error in strict mode:
arguments = []throws aSyntaxError. arguments.calleeis forbidden in strict mode. Name the function instead.
Browser support
The arguments object is supported in every browser and JavaScript runtime.