Spread operators
A spread operator looks exactly like a rest operator but performs the exact opposite function. Spread operators are used while providing arguments while calling a function or defining an array. The spread operator takes an array and splits its element into individual variables. The following example illustrates how the spread operator provides a much clearer syntax while calling functions that take an array as an argument:
function sumAll(a,b,c){ return a+b+c } var numbers = [6,7,8] //ES5 way of passing array as an argument of a function console.log(sumAll.apply(null,numbers)); //21 //ES6 Spread operator console.log(sumAll(...numbers))//21
In ES5, it is common to use the apply()
function when passing an array as an argument to a function. In the preceding example, we have an array we need to pass to a function where the function accepts three variables. The ES5 method of passing an array to...