The Arguments Object
In this lesson, you'll learn how to access all arguments passed to a function using the special arguments object.
What Is the Arguments Object?
Every regular function (not arrow functions, as you learned earlier) automatically gets a special, array-like object called arguments. This object contains all the arguments passed when the function was called, regardless of how many parameters you defined.
Think of arguments as a guest list that captures everyone who showed up to your function's party—even the uninvited ones!
How It Works
function showAll() {
console.log(arguments.length); // how many arguments passed
console.log(arguments[0]); // first argument
console.log(arguments[1]); // second argument
}
showAll("apple", "banana", "cherry");
// 3
// apple
// banana
Even though showAll() has no parameters, arguments still collects everything.
Array-Like, Not an Array
The arguments object looks like an array—you can access items with brackets and it has a length property—but it's not a real array. You can loop through it with a for loop or for...of, but array methods like map() or filter() won't work directly.
function sum() {
let total = 0;
for (let i = 0; i < arguments.length; i++) {
total += arguments[i];
}
return total;
}
console.log(sum(1, 2, 3, 4)); // 10
Key Takeaway: The arguments object gives regular functions access to all passed arguments in an array-like structure, useful for flexible functions that handle varying numbers of inputs.