DEV Community

Cover image for Rest and Spread operator
Arnold
Arnold

Posted on

Rest and Spread operator

Hello programmers,

The ES6 Rest and the Spread parameters are still very unclear to some people, and it seems to have so many people confused because they are identical. The Rest and Spread operators are represented with ... .

I still don't have any idea on why the both of them are represented with ... , honestly I feel like they should have come up with something else. So now let's dive in to what they are and what they are used for.

REST OPERATOR

If we have a function and we don't know how many arguments that will be passed in, we use the Rest operator.

Rest operator
In the image above the ... was used to handle what was passed into the function, if more values are passed into the function the Rest operator will take care of them.

SPREAD OPERATOR

The Spread operator can be used in different situations, it can also work with a function. I'll show you the different scenarios were it can be used.

Spread operator example 1
In the picture above the Spread operator was used to expand the array so it could be passed into the function.

spread operator example 2
In this image, the spread operator was used to neutralize an array so an operation could be carried out on each of the values in the array.

Basically, the Spread operator is used to neutralize an array.

Alright, this is all I have on the Rest and Spread operator. I hope you guys apply it in your new projects.

Top comments (0)