What Is Spread Operator in JavaScript?

In this article, we will discuss what is Spread Operator and how it works in JavaScript. You’ll learn how to use the Spread Operator to its true ability. While although you’ll learn about many things in this lesson, is primarily based on examples. example-driven. you will learn a lot of things from code examples.

The spread operator is a new addition to the JavaScript ES6 operator set. It takes an iterable (for example, an array) and divides it into individual elements.

To produce shallow copies of JS objects, the spread operator is often employ. Using this operation shortens the code and improves its readability.

How To Use Spread Operator

The spread operator allows an iterable to extend where 0+ arguments are anticipate. It is usually utilize in variable arrays where more than one value is require. It gives us the ability to get a list of arguments from an array. The Spread operator has the same syntax as the Rest parameter, but it operates in the other direction.


Three dots represent the spread operator…

var variablename1 = [...value]; 

In the preceding syntax… is a spread operator that targets all values in a certain variable. When… appears in a function call or similar context, it is refer to as a spread operator. Spread operators may be utilize in a variety of situations, such as when we wish to extend, copy, or concatenate a math object.

Copying An Array in JavaScript Using Spread Operator

The items of array_1 have been transfer into array_2. Changes to array_1 are not reflect in array_2, and vice versa.

When we use simple assignment operators then array_2 would have been given a reference to array_1, and any changes made in one array would have been reflected in the other, which is usually desired.

Example 01:

Spread Operator in JavaScript Copy_array_javaScript


Spread Operator in JavaScript Copy_array_javaScript_output

Copy Elements of An Array into Other Array in JavaScript

The spread operator may be use to add one array after any element of the second array, as shown. In other words, there is no restriction that fruits can only be appended to the extended_fruits array at the beginning or end.

Example 02:

Spread Operator in JavaScript Copy_array_javaScript_other



That’s all for this article if you have any queries please contact us through our website or email us at [email protected]

Leave a Comment