JavaScript ES6 Three Dots Syntax (…): Spread vs. Rest Operators

JavaScript ES6 Three Dots Syntax (…): Spread vs. Rest Operators

In this post we will give you information about JavaScript ES6 Three Dots Syntax (…): Spread vs. Rest Operators. Hear we will give you detail about JavaScript ES6 Three Dots Syntax (…): Spread vs. Rest OperatorsAnd how to use it also give you demo for it if it is necessary.

ES6 has introduced the three dots (...) syntax in JavaScript and Node which can be used, depending on the context, as a Spread operator or a Rest operator. Let’s see the difference between these two operators in more details.

In a previous post, we’ve seen what’s the Spread operator in JavaScript and that we can use it to expand or unpack the elements of a JavaScript iterable in an appropriate place like inside another array or object literal.

The Rest operator uses the same syntax but does the opposite of the Spread operator. It reduces or collects values into an array.

The best way to grasp the difference between the two operators is through some examples.

The Spread Operator & the apply() method

Let’s assume, you want to print the elements of the following array in the console:


You would call the console.log.apply() method as follows:


That’s because using the console.log() directly will print the array as a single object.

In ES6, the Spread oparator allows you to do the same but in a much readable way:


We spread the values of the letters array and we print them, just like if we are doing something like:


Deep Copying an Array with the Spread Operator

We can use the Spread operator to copy an array into another. For example:


The letters2 array is a deep copy of the letters array which means we can change the letters2 array without affecting the first array.

Read how to convert a string to the corresponding array of numbers in JavaScript

Concatenating Arrays with the Spread Operator

We can use the Spread operator to concatenate two or more arrays into one array. Since the Spread operator unpack an array when it’s applied to it we can do something like:


We simply extract the values of the a and b arrays into the new ab array.

Using the Spread Operator for Passing function arguments

Let’s suppose, you have a function that expects many individual arguments. For example, let’s this one:


And we have those values that we want to print in some array:


Instead of referencing each array member and passing it as a single argument which is quite tedious:


We can simply use the Spread operator like the following:


Transforming a String into an Array with the Spread Operator

The Spread operator works with any iterable such as literal JavaScript objects and also strings. So, we can make use of its magic to quickly transform a string into an array of characters. For example:

conststr="my string";conststrarr=[...str];

The Rest Operator in JavaScript

As we mentionned before, both the Spread and Rest operator uses the three dots syntax prepended at the name of the symbol.

In the previous section, we’ve seen various examples of the Spread operator which expands iterables into their elements. Now, let’s see the Rest operator by example.

Just like its name suggests, the Rest operator collects the rest or the remaining of elements into an array.

Using Variarble Arguments (Variadic) Functions in JavaScript

Let’s suppose, you want to define a function that could take a variable number of arguments. You would use the arguments object:


The arguments object is an array-like object, as such as you can not use Array methods with it. You also can not use with Arrow functions which are now very common between JS developers to define functions.

A better alternative is to use the Rest operator. For example:



In this quick post, we’ve seen the difference between the Spread and Rest operators in JavaScript (ES6) which both makes use of the same three dots syntax. As a recap, you can use the Spread operator if you like to expand (unpack) the elements of an iterable like object, array or string literal. In the contrary, you can use the Rest operator to collect multiple elements into an array.

Hope this code and post will helped you for implement JavaScript ES6 Three Dots Syntax (…): Spread vs. Rest Operators. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US