Why are rest parameters in JavaScript called so?
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.
The rest parameter, however, is a real array object. As such, you can use all array methods on it.
The rest operator (…) allows us to call a function with any number of arguments and then access those excess arguments as an array. The rest operator also allows us in destructuring array or objects.
Rest Parameter collects all remaining elements into an array. Spread Operator expands collected elements such as arrays into single elements. The spread syntax only does a shallow copy one level deep. Only the last parameter can be a rest parameter.
They are called rest
parameters because they capture the rest of the parameters when the function was invoked.
function multiply(multiplier, ...theArgs) {
return theArgs.map(function (element) {
return multiplier * element;
});
}
Example from https://developer.mozilla.org/en-US/docs/JavaScript/Reference/rest_parameters#Browser_compatibility
...theArgs
captures parameters 2+. In other words, multiplier
is the first parameter and theArgs
is the rest.
The word "rest" is used there to mean a container for the rest of the argument values, of which there may be any number.
It's historical usage possibly starting with Lisp Machine Lisp, definitely documented in the 1981 third edition of the Lisp Machine Manual. There were no "rest parameters" by that behavior or that name in Maclisp or Interlisp, both in 1974. Rest parameters in Common Lisp at present have the same syntax as they had in the Lisp Machine Manual. http://www.lispworks.com/documentation/HyperSpec/Body/03_dac.htm
The phrase "rest parameters" is first introduced in relation to ECMAScript in the 2012-07-12 ECMAScript 6 draft. It seems clear that the phrase there is to be understood as common parlance previously established by Lisp. If it really matters, I suppose we could ask the secretary of ECMA Technical Committee 39, Dr. Istvan Sebestyen, whose address is his first name at ecma-international.org, whether anyone would be willing to say that in so many words.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With