0 day streak
javascript / beginner
Snippet

Combining Arrays with Spread Syntax

The spread operator (...) allows you to quickly merge multiple arrays into a new one. This is a best practice in modern JavaScript to ensure you don't mutate the original data.

snippet.js
javascript
1
const allItems = [...oldItems, ...newItems];
nextjs
Breakdown
1
...oldItems
Unpacks all elements from the first array.
2
...newItems
Unpacks all elements from the second array.
3
[]
Wraps all unpacked items into a brand new array.