Modern JavaScript
About Lesson

In this JavaScript lesson we are going to talk about JavaScript Spread Operator, so it is used with arrays and its syntax is exactly the same as that of Rest Operator. and it is used to split the content of an array.

 

 

Let’s create an example.

 

 

Also we can use with the strings.

 

 

Some time we need to convert a string in to list of characters,  than we can use spread operator.

 

 

We can use spread operator with the objects.

 

 

JavaScript Spread Operator Features

Spread operator in JavaScript is represented by three dots (…) and is used to expand an iterable such as an array or an object into individual elements. these are some features of spread operator in JavaScript along with commented code examples:

 

 

  1. Copying Arrays: The spread operator can be used to make shallow copy of an array.

In this example, the spread operator is used to create new array with the same elements as the original array numbers.

 

  1. Merging Arrays: the spread operator can be used to merge two or more arrays into single array.

In this example, the spread operator is used to combine the elements of numbers1 and numbers2 into new array called allNumbers.

 

 

  1. Passing Arguments: The spread operator can be used to pass the elements of an array as arguments to function.

In this example the spread operator is used to pass the elements of the numbers array as individual arguments to the sum function.

 

  1. Copying Objects: The spread operator can be used to make a shallow copy of an object.

In this example, the spread operator is used to create a new object with the same properties as the original object person.

 

 

Overall, the spread operator in JavaScript provides a lot of flexibility and can simplify code in many situations.