Spread 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.

Is used for array construction and destructuring, and to fill function arguments from an array on invocation. A case when the operator spreads the array (or iterable object) elements.

Examples

var seasons = ['spring', 'summer', 'autumn'];

// We can add a seasons in the beginning with

seasons = ['winter', ...seasons]; // 'winter', 'spring', 'summer', 'autumn'

// Or at the end with

seasons = [...seasons, 'winter']; // 'spring', 'summer', 'autumn', 'winter'

We can used too for concat 2 arrays in 1

var dogs = ['dog1', 'dog2'];
var cats = ['cat1', 'cat2'];

var animals = [...dogs, ...cats];

console.log(animals); // ['dog1', 'dog2', 'cat1', 'cat2']

results matching ""

    No results matching ""