Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
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.
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
| 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 |
It's similar to an array because:
lengthBut:
Arraymap, forEach, filter methods, etc.const args = Array.from(arguments);
// or
const args2 = [...arguments]; // only works in regular function
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.
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).