loading...

re: 5 Uses for the Spread Operator VIEW POST

FULL DISCUSSION
 

Great stuff. Note that spread operator works also in deconstruction too, not only construction:

Take first (or n first) fields of an array:

let [first, ...rest] = [1, 2, 3, 4, 5];
// first == 1
// rest == [2, 3, 4, 5]

Or as function definition:

function giveMeAllTheParams(...params) {
  console.log(`You gave me ${params.length} params`);
}

giveMeAllTheParams(1, 2, 3, 4, 5);
// You gave me 5 params
 

Likewise for objects, can be used to create a new one that omits certain properties.

const {a, ...rest} = {a: 1, b: 2, c: 3};
 

All great examples!

 

I always forget this since there seem to be more places to use the construction functionality. Thanks for the reminder!

 
Code of Conduct Report abuse