Spread Operator in Javascript

in #coding6 years ago

Spread Operator in javascript is new new syntax introduced as part of ES6 and new features are very dynamic and they are just three dots which can do wonder.

Suppose you have to pass parameters in function which are inside the array you can simple do it like this.

The old way:

function mFunc (x, y, z) { }
var arr = [0, 1, 2];

// Call the function, passing args
mFunc.apply(null, arr);
Using Spread Operator:

mFunc(...arr);
As we can see that the code is way sorter and cleaner is we do it by using spread operator.

We can also combine two arrays simple by using spread operator

var first = ['two', 'three'];
var second= ['one', ...first];

// ["one", "two", "three"]
We can also do other amazing stuff by spread operator like copying one array in other and combining arrays.

Thankyou for reading.