Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
107 lines (82 sloc) 2.7 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs ms.assetid caps.latest.revision author ms.author manager
Spread Operator (...) (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
JavaScript
TypeScript
DHTML
10263a4c-bd27-4d87-9917-fb4b6bf373db
5
mikejo5000
mikejo
ghogen

Spread Operator (...) (JavaScript)

Allows parts of an array literal to be initialized from an iterable expression (such as another array literal), or allows an expression to be expanded to multiple arguments (in function calls).

Syntax

var array = [[arg0ToN ,] ...iterable [, arg0ToN]]  
func([args ,] ...iterable [, args | ...iterable])  
  

Parameters

iterable
Required. An iterable object.

arg0ToN
Optional. One or more elements of an array literal.

args
Optional. One or more arguments to a function.

Remarks

For more information on iterators, see Iterators and Generators. For more information on using the spread operator as a rest parameter, see Functions.

Example

In this following code example, the use of the spread operator is contrasted with the use of the concat method.

var a, b, c, d, e;  
a = [1,2,3];  
b = "dog";  
c = [42, "cat"];  
  
// Using the concat method.  
d = a.concat(b, c);  
  
// Using the spread operator.  
e = [...a, b, ...c];  
  
console.log(d);  
console.log(e);  
  
// Output:  
// 1, 2, 3, "dog", 42, "cat"  
// 1, 2, 3, "dog", 42, "cat"  

Example

The following code example shows how to use the spread operator in a function call. In this example, two array literals are passed to the function using the spread operator, and the arrays are expanded to multiple arguments.

function f(a, b, c, x, y, z) {  
  return a + b + c + x + y + z;  
}  
  
var args = [1, 2, 3];  
console.log(f(...args, 4, ...[5, 6]));  
  
// Output:  
// 21  
  

Example

With spread operators, you can simplify code that previously required the use of apply.

function f(x, y, z) {  
    return x + y + z;  
}  
  
var args = [1, 2, 3];  
  
// Old method  
f.apply(this, args);  
// New method  
f(...args);  
  

Requirements

[!INCLUDEjsv12]

See Also

Operator Precedence
Operator Summary (JavaScript)