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.
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.
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.