logo

The Arguments Object

December 26, 2020

The arguments object is available in every function you write, except arrow functions. It contains all the arguments passed to it.

"Array-like" means the arguments has a length property and properties indexed from zero but it doesn't have Array's built-in methods like filter() and map(). If we want to use array methods, we have to turn it into one.

function sum() {
  const argsArray = [...arguments];
  return argsArray.reduce((total, currentValue) => {
    return total + currentValue;
  });
}

sum(2, 5, 8); // 15

The arguments object is not available inside of arrow functions.

const multiply = () => {
  console.log(arguments);
};

multiply(5, 3); // Uncaught ReferenceError: arguments is not defined