arguments Object
arguments Object
Section titled “arguments Object”Introduction
Section titled “Introduction”The arguments object is an array-like object accessible inside functions that contains all arguments passed to the function. It’s available in regular functions (not arrow functions).
Example
Section titled “Example”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)); // 10Array-like, Not an Array
Section titled “Array-like, Not an Array”function test() { console.log(arguments.length); // 3 console.log(arguments[0]); // 'a' console.log(arguments[1]); // 'b'
// ❌ arguments.map is not a function // ✅ Convert to array const args = Array.from(arguments); // or const args = [...arguments];}
test('a', 'b', 'c');Not Available in Arrow Functions
Section titled “Not Available in Arrow Functions”const arrowFn = () => { console.log(arguments); // ReferenceError};Summary
Section titled “Summary”argumentscontains all passed arguments- It’s array-like (has length, indexed), not a real array
- NOT available in arrow functions
- Prefer rest parameters
...argsin modern code