What is arguments Pseudo-array in JavaScript
arguments is a built-in object available inside functions, containing all arguments passed to the function, regardless of whether they were explicitly declared in the signature.
It exists only in regular (not arrow) functions and allows working with a variable number of arguments.
Usage Example
function sum() {
console.log(arguments); // Pseudo-array
let total = 0;
for (let i = 0; i < arguments.length; i++) {
total += arguments[i];
}
return total;
}
console.log(sum(1, 2, 3)); // 6
arguments Object Features
| Characteristic | Value |
|---|---|
| Type | object |
| Has length | yes |
| Indexing | like array ([0], [1]) |
| Array methods | no (needs conversion) |
| Only for function | arrow functions don't have arguments |
Why is it a Pseudo-array?
It's similar to an array because:
- has indices
- has
length
But:
- it's not a real
Array - doesn't have
map,forEach,filtermethods, etc.
Converting to Array
const args = Array.from(arguments);
// or
const args2 = [...arguments]; // only works in regular function
In Arrow Functions
const foo = () => {
console.log(arguments); // ❌ ReferenceError
};
foo(1, 2, 3);
arguments doesn't exist in arrow functions because arrow functions don't create their own context.
Modern Alternative — Rest Parameters
function sum(...args) {
return args.reduce((acc, curr) => acc + curr, 0);
}
Important:
The arguments object is outdated practice. In modern projects use rest parameters (...args).