DEV Community

Omar
Omar

Posted on

Function Arguments Object in JS and how to use it

Assuming we all know what arguments are in JS functions

Function.arguments

The arguments object is a local variable available within all non-arrow functions.You can refer to a function's arguments inside that function by using its arguments object.
It has entries for each argument the function was called with, with the first entry's index at 0.

Example:

function exampleFunction(a, b, c) {
   console.log(arguments) // Arguments {0: 1, 1: 2}
  if (arguments.length !== 3) {
    throw new Error(`Invalid amount of arguments. Must be 3 and was ${arguments.length}`);
  }
}

exampleFunction(1,2) // Error: Invalid amount of arguments. Must be 3 and was 2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)