How JavaScript rest parameters actually work
Published in
3 min readDec 20, 2017
My last article covered spread syntax and Object.assign
in detail, but glossed over rest parameters in the interest of time. I do, however, feel they deserve a closer look.
Let’s begin at the trusty MDN Docs:
The rest parameter syntax allows us to represent an indefinite number of arguments as an array.
That last part, “as an array”, is interesting, because before ES6 arrow functions, we used the arguments
object. It was array-like, but not actually an array.
Example:
function returnArgs() {
return arguments;
}
We see arguments
has indices, so it’s loop-able:
function loopThruArgs() {
let i = 0; for (i; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
But it’s not an array.