DEV Community

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

Posted on

6 1

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay