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 1123299 of Spread syntax

  • Revision slug: Web/JavaScript/Reference/Operators/Spread_operator
  • Revision title: Spread syntax
  • Revision id: 1123299
  • Created:
  • Creator: deveedutta
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Operators")}}

The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables  (for destructuring assignment) are expected.

Syntax

For function calls:

myFunction(...iterableObj);

For array literals:

[...iterableObj, 4, 5, 6]

Examples

A better apply

Example: it is common to use {{jsxref( "Function.prototype.apply")}} in cases where you want to use an array as arguments to a function.

function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction.apply(null, args);

With ES6 spread you can now write the above as:

function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction(...args);

Any argument in the argument list can use the spread syntax and it can be used multiple times.

function myFunction(v, w, x, y, z) { }
var args = [0, 1];
myFunction(-1, ...args, 2, ...[3]);

A more powerful array literal

Example: Today if you have an array and want to create a new array with the existing one being part of it, the array literal syntax is no longer sufficient and you have to fall back to imperative code, using a combination of push, splice, concat, etc. With spread syntax this becomes much more succinct:

var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes']; // ["head", "shoulders", "knees", "and", "toes"]

Just like with spread for argument lists ... can be used anywhere in the array literal and it can be used multiple times.

Apply for new

Example: In ES5 it is not possible to compose new with apply. (In ES5 terms, apply does a [[Call]] and not a [[Construct]].) In ES6 the spread syntax naturally supports this:

var dateFields = readDateFields(database);
var d = new Date(...dateFields);

Copy an array

var arr = [1,2,3];
var arr2 = [...arr]; // like arr.slice()
arr2.push(4); // arr2 becomes [1,2,3,4], arr stays unaffected

Note: Typically the spread operators in ES6 goes one level deep while copying an array. Therefore, they are unsuitable for copying multidimensional arrays. It's the same case with Object.assign and Object spread operators. Look at the example below for a better understanding.

Example:

var a =[[1], [2], [3]];
var b = [...a];
b.shift().shift();// a is now [[], [2], [3]]

A better push

Example: {{jsxref("Global_Objects/Array/push", "push")}} is often used to push an array to the end of an existing array. In ES5 this is often done as:

var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
// Append all items from arr2 onto arr1
Array.prototype.push.apply(arr1, arr2);

In ES6 with spread this becomes:

var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1.push(...arr2);

Only apply for iterables

var obj = {"key1":"value1"};
function myFunction(x) {
    console.log(x); // undefined
}
myFunction(...obj);
var args = [...obj];
console.log(args, args.length) //[] 0

Rest operator

Rest operator looks exactly like the spread syntax, and is used for destructuring arrays and objects. In a way, Rest elements are the opposite of spread elements - spread elements 'expands' an array into its elements, and rest elements collects multiple elements and 'condenses' into a single element.

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-array-initializer')}} {{Spec2('ES6')}} Defined in several sections of the specification: Array Initializer, Argument Lists
{{SpecName('ESDraft', '#sec-array-initializer')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Spread operation in array literals {{CompatChrome("46")}} {{ CompatGeckoDesktop("16") }} {{CompatIE("Edge")}} {{CompatNo}} 7.1
Spread operation in function calls {{CompatChrome("46")}} {{ CompatGeckoDesktop("27") }} {{CompatIE("Edge")}} {{CompatNo}} 7.1
Spread operation in destructuring {{CompatChrome("49")}} {{ CompatGeckoDesktop("34") }} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Spread operation in array literals {{CompatNo}} {{CompatChrome("46")}} {{ CompatGeckoMobile("16") }} {{CompatNo}} {{CompatNo}} 8 {{CompatChrome("46")}}
Spread operation in function calls {{CompatNo}} {{CompatChrome("46")}} {{ CompatGeckoMobile("27") }} {{CompatNo}} {{CompatNo}} 8 {{CompatChrome("46")}}
Spread operation in destructuring {{CompatNo}} {{CompatNo}} {{ CompatGeckoDesktop("34") }} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatNo}}

See also

Revision Source

<div>{{jsSidebar("Operators")}}</div>

<p>The <strong>spread syntax</strong> allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables&nbsp; (for&nbsp;destructuring assignment)&nbsp;are expected.</p>

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

<p>For function calls:</p>

<pre class="brush: js">
myFunction(...iterableObj);
</pre>

<p>For array literals:</p>

<pre class="brush: js">
[...iterableObj, 4, 5, 6]</pre>

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

<h3 id="A_better_apply">A better apply</h3>

<p><strong>Example:</strong> it is common to use {{jsxref( "Function.prototype.apply")}} in cases where you want to use an array as arguments to a function.</p>

<pre class="brush: js">
function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction.apply(null, args);</pre>

<p>With ES6 spread you can now write the above as:</p>

<pre class="brush: js">
function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction(...args);</pre>

<p>Any argument in the argument list can use the spread syntax and it can be used multiple times.</p>

<pre class="brush: js">
function myFunction(v, w, x, y, z) { }
var args = [0, 1];
myFunction(-1, ...args, 2, ...[3]);</pre>

<h3 id="A_more_powerful_array_literal">A more powerful array literal</h3>

<p><strong>Example:</strong> Today if you have an array and want to create a new array with the existing one being part of it, the array literal syntax is no longer sufficient and you have to fall back to imperative code, using a combination of <code>push</code>, <code>splice</code>, <code>concat</code>, etc. With spread syntax this becomes much more succinct:</p>

<pre class="brush: js">
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes']; // <span class="objectBox objectBox-array" role="presentation"><a class="objectLink "><span class="arrayLeftBracket" role="presentation">[</span></a><span class="objectBox objectBox-string" role="presentation">"head"</span><span class="arrayComma" role="presentation">, </span><span class="objectBox objectBox-string" role="presentation">"shoulders"</span><span class="arrayComma" role="presentation">, </span><span class="objectBox objectBox-string" role="presentation">"knees"</span><span class="arrayComma" role="presentation">, </span><span class="objectBox objectBox-string" role="presentation">"and"</span><span class="arrayComma" role="presentation">, </span><span class="objectBox objectBox-string" role="presentation">"toes"</span></span>]
</pre>

<p>Just like with spread for argument lists <code>...</code> can be used anywhere in the array literal and it can be used multiple times.</p>

<h3 id="Apply_for_new">Apply for new</h3>

<p><strong>Example:</strong> In ES5 it is not possible to compose <code>new</code> with <code>apply.</code> (In ES5 terms,&nbsp;<code>apply</code> does a <code>[[Call]]</code> and not a <code>[[Construct]].</code>)&nbsp;In ES6 the spread syntax naturally supports this:</p>

<pre class="brush: js">
var dateFields = readDateFields(database);
var d = new Date(...dateFields);</pre>

<h3 id="Copy_an_array">Copy an array</h3>

<pre class="brush: js">
var arr = [1,2,3];
var arr2 = [...arr]; // like arr.slice()
arr2.push(4); // arr2 becomes [1,2,3,4], arr stays unaffected
</pre>

<p><strong>Note:</strong> Typically the&nbsp;spread operators in ES6 goes one level deep while copying an array. Therefore, they are unsuitable for copying multidimensional arrays.&nbsp;It's the same case with <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign">Object.assign</a>&nbsp;and Object spread operators. Look at the example below for a better understanding.</p>

<p><strong>Example:</strong></p>

<pre class="brush: js">
var a =[[1], [2], [3]];
var b = [...a];
b.shift().shift();// a is now [[], [2], [3]]
</pre>

<h3 id="A_better_push">A better push</h3>

<p><strong>Example:</strong> {{jsxref("Global_Objects/Array/push", "push")}} is often used to push an array to the end of an existing array. In ES5 this is often done as:</p>

<pre class="brush: js">
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
// Append all items from arr2 onto arr1
Array.prototype.push.apply(arr1, arr2);</pre>

<p>In ES6 with spread this becomes:</p>

<pre class="brush: js">
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1.push(...arr2);</pre>

<h3 id="Only_apply_for_iterables">Only apply for&nbsp;iterables</h3>

<pre class="brush: js">
var obj = {"key1":"value1"};
function myFunction(x) {
&nbsp;&nbsp; &nbsp;console.log(x); // undefined
}
myFunction(...obj);
var args = [...obj];
console.log(args, args.length) //[] 0</pre>

<h2 id="Rest_operator">Rest operator</h2>

<p>Rest operator looks exactly like the spread syntax, and is used for destructuring arrays and objects. In a way, Rest elements are&nbsp;the opposite of spread elements - spread elements 'expands'&nbsp;an array into its elements, and rest elements collects multiple elements and 'condenses' into a single element.</p>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ES6', '#sec-array-initializer')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Defined in several sections of the specification: <a href="https://www.ecma-international.org/ecma-262/6.0/#sec-array-initializer">Array Initializer</a>, <a href="https://www.ecma-international.org/ecma-262/6.0/#sec-argument-lists">Argument Lists</a></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array-initializer')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

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

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Spread operation in array literals</td>
   <td>{{CompatChrome("46")}}</td>
   <td>{{ CompatGeckoDesktop("16") }}</td>
   <td>{{CompatIE("Edge")}}</td>
   <td>{{CompatNo}}</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td>Spread operation in function calls</td>
   <td>{{CompatChrome("46")}}</td>
   <td>{{ CompatGeckoDesktop("27") }}</td>
   <td>{{CompatIE("Edge")}}</td>
   <td>{{CompatNo}}</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td>Spread operation in destructuring</td>
   <td>{{CompatChrome("49")}}</td>
   <td>{{ CompatGeckoDesktop("34") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</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>Spread operation in array literals</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome("46")}}</td>
   <td>{{ CompatGeckoMobile("16") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
   <td>{{CompatChrome("46")}}</td>
  </tr>
  <tr>
   <td>Spread operation in function calls</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome("46")}}</td>
   <td>{{ CompatGeckoMobile("27") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
   <td>{{CompatChrome("46")}}</td>
  </tr>
  <tr>
   <td>Spread operation in destructuring</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatGeckoDesktop("34") }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

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

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/rest_parameters">Rest parameters</a></li>
 <li><a href="https://exploringjs.com/es6/ch_destructuring.html#sec_rest-operator">Rest operator</a></li>
</ul>
Revert to this revision