0 day streak
javascript / beginner
Snippet

Expanding Arrays with the Spread Operator

The spread operator (...) allows you to quickly copy all elements from one array into another. This is useful for creating new versions of data without changing the original.

snippet.js
javascript
1
2
3
4
const baseColors = ["red", "green"];
const allColors = [...baseColors, "blue", "yellow"];
// allColors is ["red", "green", "blue", "yellow"]
nextjs
Breakdown
1
...baseColors
Spreads or 'unpacks' the elements of baseColors into the new array.
2
"blue", "yellow"
New elements added to the end of the new array.