Spread Operator in JavaScript

Today we will explain you about the spread operator in JavaScript. Spread Operator is introduced by JavaScript ES6 (ECMAScript 6). The syntax is three dots(…) followed by the array (or iterable*). Spread operator allows iterables(arrays/objects/strings) to be expanded into single arguments/elements.

Spread Operator in JavaScript, Understanding the Spread Operator in JavaScript, Difference between Rest and Spread Operator in JavaScript ES6 (ECMAScript 6). How to use three or triple dots(…) in JavaScript.

Use of Spread Operator

  1. Immutable copy an array or object
  2. Concatenating arrays or objects
  3. Spreading elements together with an individual element
  4. Spreading elements in function arguments

1. Immutable copy an array or object

With the use of spread operator we can create the immutable object or array which will not affect on another variable.

Same things you can manage it for object as well.

2. Concatenating arrays or objects

If you want to concat the two arrays or objects then you can use the spread operator.

Let’s take an example for an object. If your object attribute is same then it will be update the new value in attribute.

In the above example A attribute is exist in both objects. So when you merge it then it will update the new value as 7 in A attribute.

3. Spreading elements together with an individual element

Here we are spreading elements together with an individual element and get the updated array.

4. Spreading elements in function arguments

We can also spread the elements in function arguments by the use of spread operator.

Thanks for reading. Happy Coding!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *