DEV Community

Cover image for Spread Operator(...) in JavaScript.
Uddesh
Uddesh

Posted on

Spread Operator(...) in JavaScript.

Hello again, Just a quick reminder, This is the third part of the series Strange JS so if you haven't checked out yet go and read previous two posts.

So without further wasting time let's get started. Today we are gonna talk about Spread Operator but I call them Super Dots because I like it. 🙃

Now the biggest question is What the hack is Spread Operator??

In simplest words Spread Operator convert a list into an array and an array into a list. Sounds confusing?

Let's understand with some examples. Assume that you created a function which takes 2 or 3 arguments and you just called that function but you have an array and function needs a list. for example.

function sum(a, b, c) {
    console.log(a + b + c)
}

let array = [1, 2, 3]
sum(...array)

// 6
Enter fullscreen mode Exit fullscreen mode

This was the one scenario, but it can be used for a variety of cases like array manipulation.

You can concat two arrays.

const num = [1,2,3,4]
const words = ['Hey', 'Hellow']

console.log([...num, ...words])

//[1, 2, 3, 4, "Hey", "Hellow"]
Enter fullscreen mode Exit fullscreen mode

These are only two examples but it can be lots of use cases so get your hands dirty with Spread Operator.

I'll be back with something new and strange until then Good bye.
Bye

Latest comments (0)