arguments
is an array-like object available within Javascript functions. As its name suggests, arguments
contains the values of the arguments passed to the function within which it is accessed.
Similar to arrays, the arguments object has a length property. In addition, its values are ordered, and can be retrieved via index:
Unlike arrays, arguments
does not have any built-in array methods like map(), filter(), splice(), etc.
Nevertheless, we can convert arguments
into an array easily:
This information is particularly useful when working with functions that are called with more arguments than they have been declared to accept. It’s also useful for creating functions that can be passed a variable number of arguments:
Arrow Functions and the rest
Parameter
Arrow functions do not have an arguments binding in their scope. In other words, no arguments object
is created they are called.
However, if you do want to capture arguments in an arrow function, you can use the rest parameter.
With the rest parameter (…args), the memoize function below will accept a function as a parameter, and call it with whatever number of arguments are provided. This pattern allows memoize to be incredible flexible in it’s application.
Another familiar example is the debounce method. Here, a generic callback function can again accept any number of arguments:
Rest parameters can also be combined with other parameters, but must always be included as the last parameter