susanahernandezd
4/22/2019 - 8:13 AM

Three dots (spread operator & rest operator)

Math.min(...[1,2,3,4]) is the same as Math.min(1,2,3,4)

//1. The REST OPERATOR is used to get the arguments list passed to function on invocation and in array destructure.
// get the number of arguments
function countArguments(...args) {  
   return args.length;
}
countArguments('welcome', 'to', 'Earth'); // => 3 

// destructure an array
let otherSeasons, autumn;  
[autumn, ...otherSeasons] = ['autumn', 'winter'];
otherSeasons      // => ['winter']  


//2. The SPREAD OPERATOR 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.

let cold = ['autumn', 'winter'];  
let warm = ['spring', 'summer'];  
// construct an array
[...cold, ...warm] // => ['autumn', 'winter', 'spring', 'summer']
// function arguments from an array
cold.push(...warm);  
cold              // => ['autumn', 'winter', 'spring', 'summer']