ES6 - Spread Operator


// ES6 - Spread operator:

function length(...nums) {
length(1,2,3); // 3

The 3 dots is called the spread operator, and it is used to take in any number 
of arguments and convert them into an array.

With the spread operator, we no longer need to create an array from the 
arguments object.  The spread operator also serves as a handy shortcut to the 
case where we have got an array that we need to pass as arguments to another 
function.  For example, if we have a function named total that takes 3 

function total(x, y, z) {
  return x + y + z;

and we have an array:

var a = [1, 2, 3];

The current way to do this is to use apply:

total.apply(null, a);

However, now we can use the spread operator to do exactly the same thing:


Just simply put the spread operator in front of the array variable.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License