Spread Operator in JS

I didn’t understand the use of Spread Operator.Can anyone explain me about it…like how it makes array expands??

let’s say you see this:
var oldArray = [1,2,3,4];
var newArray = […oldArray];

then you run
the result will be that newArray has the exact same values as oldArray

This is because of the spread operator ‘…’
The javascript interpreter reads it and then understands you want to take the values of the old array and ‘spread them out’ (like butter on toast) so it effectively does this:
var newArray = [1,2,3,4];

not sure if this is a good enough explanation for you…

thanxxx…it was helpful
I was a little bit confused between rest and spread operator.

1 Like

Not just for arrays!

const a = {x: 1, y: 2, z: 3}
const b = {...a}
1 Like