Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 900817 of Rest parameters

  • Revision slug: Web/JavaScript/Reference/Functions/rest_parameters
  • Revision title: Rest parameters
  • Revision id: 900817
  • Created:
  • Creator: Llbe
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Functions")}} {{Harmony}}

The rest parameter syntax allows to represent an indefinite number of arguments as an array.

Syntax

function(a, b, ...theArgs) {
  // ...
}

Description

If the last named argument of a function is prefixed with ..., it becomes an array whose elements from 0 to theArgs.length are supplied by the actual arguments passed to the function.

In the above example, theArgs would collect the third argument of the function (because the first one is mapped to a, and the second to b) and all the consecutive arguments.

Difference between rest parameters and the arguments object

There are three main differences between rest parameters and the arguments object:

  • rest parameters are only the ones that haven't been given a separate name, while the arguments object contains all arguments passed to the function;
  • the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly;
  • the arguments object has additional functionality specific to itself (like the callee property).

From arguments to an array

Rest parameters have been introduced to reduce the boilerplate code that was induced by the arguments

// Before rest parameters, the following could be found:
function f(a, b){
  var args = Array.prototype.slice.call(arguments, f.length);

  // ...
}

// to be equivalent of

function(a, b, ...args) {
  
}

Examples

Since theArgs is an array, you can get the count of its elements by using the length property:

function fun1(...theArgs) {
  console.log(theArgs.length);
}

fun1();  // 0
fun1(5); // 1
fun1(5, 6, 7); // 3

In the next example, we use the rest parameters to collect arguments from the second one to the end. We then multiply them by the first one:

function multiply(multiplier, ...theArgs) {
  return theArgs.map(function (element) {
    return multiplier * element;
  });
}

var arr = multiply(2, 1, 2, 3); 
console.log(arr); // [2, 4, 6]

The following example shows that you can use Array methods on rest params, but not on the arguments object:

function sortRestArgs(...theArgs) {
  var sortedArgs = theArgs.sort();
  return sortedArgs;
}

console.log(sortRestArgs(5,3,7,1)); // shows 1,3,5,7

function sortArguments() {
  var sortedArgs = arguments.sort(); 
  return sortedArgs; // this will never happen
}

// throws a TypeError: arguments.sort is not a function
console.log(sortArguments(5,3,7,1));

In order to use Array methods on the arguments object, you would need to convert it to a real array first.

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-function-definitions', 'Function Definitions')}} {{Spec2('ES6')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(45)}} {{CompatVersionUnknown}} {{CompatGeckoDesktop("15.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatChrome(45)}} {{CompatGeckoMobile("15.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatChrome(45)}}

See also

Revision Source

<div>{{jsSidebar("Functions")}} {{Harmony}}</div>

<p>The <strong>rest parameter</strong> syntax allows to represent an indefinite number of arguments as an array.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="brush: js">
function(a, b, ...theArgs) {
  // ...
}
</pre>

<h2 id="Description">Description</h2>

<p>If the last named argument of a function is prefixed with <code>...</code>, it becomes an array whose elements from <code>0</code> to <code>theArgs.length</code> are supplied by the actual arguments passed to the function.</p>

<p>In the above example, <code>theArgs</code> would collect the third argument of the function (because the first one is mapped to <code>a</code>, and the second to <code>b</code>) and all the consecutive arguments.</p>

<h3 id="Difference_between_rest_parameters_and_the_arguments_object">Difference between rest parameters and the <code>arguments</code> object</h3>

<p>There are three main differences between rest parameters and the <a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments" title="arguments"><code>arguments</code></a> object:</p>

<ul>
 <li>rest parameters are only the ones that haven't been given a separate name, while the <code>arguments</code> object contains all arguments passed to the function;</li>
 <li>the <code>arguments</code> object is not a real array, while rest parameters are <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" title="Array"><code>Array</code></a> instances, meaning methods like <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" title="Array sort method"><code>sort</code></a>, <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" title="Array map method"><code>map</code></a>, <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach" title="Array forEach method"><code>forEach</code></a> or <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop" title="Array pop method"><code>pop</code></a> can be applied on it directly;</li>
 <li>the <code>arguments</code> object has additional functionality specific to itself (like the <code>callee</code> property).</li>
</ul>

<h3 id="From_arguments_to_an_array">From arguments to an array</h3>

<p>Rest parameters have been introduced to reduce the boilerplate code that was induced by the arguments</p>

<pre class="brush: js">
// Before rest parameters, the following could be found:
function f(a, b){
  var args = Array.prototype.slice.call(arguments, f.length);

  // ...
}

// to be equivalent of

function(a, b, ...args) {
  
}
</pre>

<h2 id="Examples">Examples</h2>

<p>Since <code>theArgs</code> is an array, you can get the count of its elements by using the <code>length</code> property:</p>

<pre class="brush: js">
function fun1(...theArgs) {
  console.log(theArgs.length);
}

fun1();  // 0
fun1(5); // 1
fun1(5, 6, 7); // 3
</pre>

<p>In the next example, we use the rest parameters to collect arguments from the second one to the end. We then multiply them by the first one:</p>

<pre class="brush: js">
function multiply(multiplier, ...theArgs) {
  return theArgs.map(function (element) {
    return multiplier * element;
  });
}

var arr = multiply(2, 1, 2, 3); 
console.log(arr); // [2, 4, 6]
</pre>

<p>The following example shows that you can use <code>Array</code> methods on rest params, but not on the <code>arguments</code> object:</p>

<pre class="brush: js">
function sortRestArgs(...theArgs) {
  var sortedArgs = theArgs.sort();
  return sortedArgs;
}

console.log(sortRestArgs(5,3,7,1)); // shows 1,3,5,7

function sortArguments() {
  var sortedArgs = arguments.sort(); 
  return sortedArgs; // this will never happen
}

// throws a TypeError: arguments.sort is not a function
console.log(sortArguments(5,3,7,1));
</pre>

<p>In order to use <code>Array</code> methods on the <code>arguments</code> object, you would need to convert it to a real array first.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-function-definitions', 'Function Definitions')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(45)}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("15.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(45)}}</td>
   <td>{{CompatGeckoMobile("15.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(45)}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments" title="arguments">Arguments object</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" title="Array">Array</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions" title="Functions and function scope">Functions</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator" title="spread operator">Spread Operator</a></li>
 <li><a class="external" href="https://wiki.ecmascript.org/doku.php?id=harmony:rest_parameters">Original proposal at ecmascript.org</a></li>
 <li><a class="external" href="https://javascriptweblog.wordpress.com/2011/01/18/javascripts-arguments-object-and-beyond/">JavaScript arguments object and beyond</a></li>
</ul>
Revert to this revision