Why are the rest parameters in JavaScript called so?

543 views Asked by At

Why are rest parameters in JavaScript called so?

2

There are 2 answers

0
minopret On

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.

0
Rick Viscomi On

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.