Skip to main content

Command Palette

Search for a command to run...

What is the Spread operator in JavaScript?

Updated
1 min read
What is the Spread operator in JavaScript?

The spread operator will copy the content of an iterable element like an array or a string. It will spread the content and won't mutate the original element. The syntax is three dots (...) and then the name of the element to copy.

Let's see an example:

const firstArray = [1,2,3]

const secondArray = [...firstArray, 4, 5]

firstArray // [ 1, 2, 3 ]
secondArray // [ 1, 2, 3, 4, 5 ]

The first element of the secondArray variable is the content of the firstArray variable. Each value will be placed on one index position and will be part of the array. Note that in the secondArray variable we also added other values to the array. This is possible in addition to the spread operator. Finally, when we see at the firstArray variable values haven't been modified.

More from this blog

Mario Barceló

8 posts

💻 Frontend Developer. 📱 Tech passionate. 🎲 Board Game lover. 🏈 Football rookie.