Q&A
What is the arguments object in JavaScript?
Overview
Before ES6, JavaScript had no clean syntax for variable-argument (variadic) functions, so every regular function automatically received an arguments object containing all passed values. It's "array-like" - indexable and has .length - but it's not a true array, so it lacks methods like map or forEach directly.
graph TD
A["function sum() { ... }"] --> B[arguments object created]
B --> C["arguments[0], arguments[1], ..."]
B --> D["arguments.length"]
B --> E[NOT a real array - array-like]
A -.modern alternative.-> F["function sum(...nums) - real array"]
style F fill:#1e1b4b,color:#fff
Basic Usage
function sum() {
let total = 0;
for (let i = 0; i < arguments.length; i++) {
total += arguments[i];
}
return total;
}
sum(1, 2, 3, 4); // 10 - works even though no parameters declared
Even with zero declared parameters, arguments captured all four values.
It's Array-Like, Not an Array
function demo() {
console.log(arguments.length); // works
console.log(arguments[0]); // works
// arguments.map(...) // ❌ TypeError: not a function
const arr = Array.from(arguments); // convert to real array
const arr2 = [...arguments]; // spread also converts
arr.map(x => x * 2); // now array methods work
}
To use array methods you must convert it first (Array.from or spread).
Arrow Functions Have No arguments
Arrow functions do not have their own arguments - they inherit it from the enclosing scope (or it's undefined at top level):
const f = () => console.log(arguments); // refers to outer scope / error
This is a key reason to know the difference between regular and arrow functions.
The Modern Replacement: Rest Parameters
ES6 rest parameters (...name) collect arguments into a real array and are clearer:
function sum(...nums) { // nums is a true Array
return nums.reduce((total, n) => total + n, 0);
}
sum(1, 2, 3, 4); // 10
Rest parameters are preferred because:
numsis a genuine array (hasmap,reduce, etc.).- They work in arrow functions.
- You can combine fixed and variadic params:
function f(first, ...rest) {}.
A Historical Quirk: Linked Parameters (sloppy mode)
In non-strict ("sloppy") mode, arguments was linked to named parameters - changing one changed the other:
function f(a) {
arguments[0] = 99;
return a; // 99 in sloppy mode (!), but unchanged in strict mode
}
Strict mode ("use strict") breaks this link. This surprising coupling is another reason arguments is discouraged.
When You Might Still See It
- Legacy/pre-ES6 codebases.
- Code that must run without transpilation in very old environments.
- Quick introspection of argument count.
For new code, use rest parameters.
Why This Matters in Interviews
JS interviews use arguments to probe understanding of variadic functions, the array-like-vs-array distinction, the arrow-function difference (no own arguments), and the modern rest-parameter alternative. Knowing the sloppy-mode parameter linkage is a deeper trivia point. The expected takeaway: "prefer ...rest, but understand arguments for legacy code."
Common Pitfalls
- Calling array methods (
map,filter) directly onarguments→ TypeError. - Expecting
argumentsinside an arrow function (it doesn't have its own). - Relying on sloppy-mode parameter↔arguments linkage (breaks in strict mode).
- Using
argumentsin new code instead of clearer rest parameters.
References
- MDN
argumentsobject: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments - MDN Rest parameters: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters