Javascript - Operator Spread Operator

Introduction

The spread operator, denoted by ... before an array, spreads out an array.

Consider the following example:

let values = [200, 300, 400]; 
let newSet = [100, ...values, 500] 

console.log(newSet);  // [100, 200, 300, 400, 500] 

...values expands the array values into newSet.

The following code uses the Math.max() method and the spread operator:

let numbers = [-25, 100, 42, -1000]; 
console.log(Math.max(...numbers, 900));        // 900 

Here, Math.max is passed five arguments, the first four being -25, 100, 42, and -1000 and 900.

The spread operator spreads out the values in an array as arguments in a function call.

The following code shows how to spread out an empty array.

The parameters spread out would also be undefined.

function printInput(...input) { 
        console.log(input); 
} 

let input = [,,]; 

printInput(...input); // [undefined, undefined]