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

Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.

See Also

Operator Precedence
Operator Summary (JavaScript)