Use Rest Operators ( … ) || ES6

What the heck are Three Dots || Rest Operator ?

Same question get arise when I was learning the concepts of ES6. So yeah, first-of-all Rest Operator is the part of ES6.

Now, how they work ? Let See quick -

CASE 1 : Dealing with Arrays.

Suppose i want only two fields ( a, b ) from Array and other fields should be in other parameter (rest).

Here Two Concepts are working together i.e Destructing and Rest Operator.

`a` , `b` , `…rest` are making array into three pieces ( DESTRUCTION ).
`…` is taking all rest things left in one variable.

CASE 2 : Dealing with Objects.

Now let say, we want data from objects.

Here Also Two Concepts are working together i.e Destructing and Rest Operator.
`a` , `b` , `…rest` are making array into three peices ( DESTRUCTION ).
In with `rest` is giving all other objects left.

CASE 3 : Dealing with Complex Objects.

Here, if i want to change the value of `a` in object.
You can do it like -

This will do in the same object !!

What if you want NEW Object having same values of above object and then change the value.

Let’s Try -

NOTE — Notice that both the Objects ( OLD ,NEW ) will be changed,
because Objects Save REFERENCES. So both objects have new updated value of `a`.

I am pretty sure you know that, just Reminding 😜

To Solve the above issue we can use the Saviour ( Rest Operator )

What this will do is ? Any idea ?

It will include whole object inside the NEW object ( as named newObject ) and then modify the key `a` if it is present, else add the new key in object.

What if i change the order -

Nothing will happen here because first we are adding a key `a` in new object then we are modifying that with old object. So this will give the same Result as Old object.

I know what you are saying 😎

I think now you are much clear about Rest Operators.

Hope you Enjoy this Article. Please clap if you Like this.

Follow Me ❤️ for more Articles on JavaScript, Angular, React, Node.

--

--

--

Founder and C.E.O at XpRate.com | Want to help world to grow 🌱

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hardeep Singh

Hardeep Singh

Founder and C.E.O at XpRate.com | Want to help world to grow 🌱

More from Medium

TypeScript Array Functions

Complete Dive Into TypeScript — Part 2

Object-Oriented Programming with TypeScript — Encapsulation

How to run Unit Tests in Angular