A relatively simple concept, but one that I feel benefits from a clear explanation.
arguments is an array-like object that lives inside all function bodies. It contains all the parameters that the function was sent when it was called. Let me illustrate:
So, as you can see, we can retrieve an array of the passed in parameters (even if more were passed in than were required). When I say “array”, it’s important to know that the
arguments that you can access is an array-like object, ie. it looks like an array, it has a
length property, and you could
while loops with it, but that’s about it.
If we wanted to make it into a real array, we can use our handy little trick of accessing the
slice method from the Array prototype and hi-jacking it for use on our
arguments object, like so:
So now we can go ahead and use
map or any other Array method we desire.
Now, use-cases. What could we do with this little oddity? Well, we can create a function that can accept any number of parameters, without having to explicitly declare them as named parameters. Like so:
Apart from re-creating REM lyrics, there is a huge potential for use-cases for the
arguments object, mostly revolving around creating functions that can accept a variable number of parameters.