In the below code,
function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction(...args);
Is spread operator(...
) unpacki
...
is known as Spread/Rest operator depending upon how and where it is used.
What does spread operator do? The spread operator allows an expression to be expanded in places where multiple elements/variables/arguments are expected.
Before ES6, arguments is an array like object, corresponds to the arguments passed to a function. Here is a quick look at its use:
(function(a, b, c){
console.log(arguments);
})(1, 2, 3);
Output would be [1,2,3].
In ES6, the same can be written as:
function getSum(x, y, z){
console.log(x+y+z);
}
can call either of two ways:
getSum(...[10,20,30]);
or
var argArr = [10,20,30];
getSume(...argArr);
We have put ...
in front of array, so it spread the elements of array into individual values.