3 ways to use the JavaScript spread operator with arrays – 30 seconds of code

Clone an array

The spread operator can be used to clone an array into a new array. This trick can come in handy when working with arrays of primitives. However, it only shallow clones the array, meaning nested non-primitive values will not be cloned.

Merge multiple arrays

Using the spread operator, it’s possible to combine two or more arrays into one. You should think of this trick as cloning two arrays into a new one. Due to that, the shallow cloning limitation mentioned previously applies here, too.

Add items to an array

Similarly to previous tricks, it’s possible to spread an array into a new one and add individual elements, too. This can also be combined with merging multiple arrays, if desired.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Article

Hummingbird Cake • Olive & Mango

Next Article

Easy Crustless Quiche - Greekstyle • Olive & Mango

Related Posts