Shubham Singh avatar

Spread Operator in Javascript

shubham165

Published: 22 Jun 2018 › Updated: 22 Jun 2018Spread Operator in Javascript

Spread Operator in Javascript

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.

Leave Spread Operator in Javascript to:

Written by

Coder | Learner | Traveller

Read more #coding posts


Best Posts From Shubham Singh

We have not curated any of shubham165's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Shubham Singh