In ES6, you can write three dots which represent the spread operator as well as the rest parameters. Now, it actually depends upon the context that you use them in. Let us first look at how you can write three dots to represent a spread operator.
In the last video, you learned how spread operator can break down an array into its individual components and quite literally, spread an array. You also looked at how you can concatenate two arrays using the spread operator. Besides these, let’s look at some more useful use-cases where the spread operator can be used.
In the last video, you looked at how the spread operator can help you to make a copy of an array. Note that you could not have simply assigned the array to another variable. In case you would have done that, it would have been pass by reference because the custom types in JavaScript are always passed by reference and this is why, whenever you would change the original array, the copied array would also get changed, which is not something that we require in this case.
Let alone arrays, the spread operator can be used on objects as well and will serve all the advantages with objects too as it does with the arrays.
Consider a scenario where you want to do the opposite - that means you want to pass individual elements into a function and have them treated as an array or an object. In such a case, the rest parameters are used. Let’s look at how you can use them in the next segment.