Menu

1 / 2

Spread vs Rest Operators in JavaScript

DEV Community·SATYA SOOTAR·6 months ago
#vNcyiIv9
#rest#spreading#javascript#spread#const
Reading 0:00
15s threshold

Hello readers 👋, welcome to the 23rd blog in this JavaScript series!

In the last post, we discovered how async/await makes asynchronous code feel as natural as synchronous code. Today, we are going to talk about a pair of operators that look identical but do completely opposite jobs: the spread and rest operators.

Both use three dots ( ... ), but one expands values out while the other collects values in. This tiny syntax is incredibly powerful and appears all over modern JavaScript. Let's unravel the mystery and see where each one shines.

What the spread operator does

Think of the spread operator as an unpacking tool. It takes an array or an object and spreads its elements or properties into individual pieces. It is like opening a box of chocolates and laying them all out on a table. The box (the array) gives you the chocolates (the individual items).

Spreading an array

Before spread, if you wanted to concatenate two arrays, you used concat .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.