Hiprup

What is the arguments object?

arguments is an array-like object available inside regular functions, holding all passed arguments regardless of the declared parameters.

  • Array-like — has length and indices, but lacks array methods like map.

  • Not in arrows — arrow functions have no arguments object.

Modern replacement: rest parameters (...args) give a real array and are clearer — prefer them.

// arguments object
function sum() {
  console.log(arguments);        // [1, 2, 3] (array-like)
  console.log(arguments.length); // 3
  console.log(arguments[0]);     // 1

  // Convert to real array
  const args = Array.from(arguments);
  return args.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6

// NOT available in arrow functions
const arrowSum = (...args) => {  // Use rest params instead
  // console.log(arguments); // ReferenceError in arrow!
  return args.reduce((a, b) => a + b, 0);
};

// Modern alternative: rest parameters (recommended)
function modernSum(...numbers) { // Real array!
  return numbers.reduce((a, b) => a + b, 0);
}

arguments is array-like (has length and indices) but lacks array methods. Array.from converts it to a real array.

Arrow functions do not have arguments — use ...rest instead. Rest parameters (...numbers) create a real array and are the modern replacement for arguments.

arguments is array-like (not a real array). Not available in arrow functions.

Rest parameters (...args) are the modern replacement. Know how to convert to array: Array.from, spread, or slice.call. arguments is legacy — always prefer rest params.

What is the arguments object? | Hiprup