JavaScript ES6 - Spread Operator And Rest Parameter

The Spread operator

 
In ES6, a spread operator is represented by the “” token without the quotes. It splits an iterable object into its individual values. For now, just bear in mind that a good example of an iterable object is an array.
 
To give a background, let’s try to see the old way of JavaScript that uses the concept of spread operator.
 
Let us see an example below.
  1. function myFunc(arg1, arg2, arg3, arg4){  
  2.     return arg1 + arg2 + arg3 + arg4;  
  3. }  
  4.   
  5. var myArgs = [1,2,3,4];  
  6. var result = myFunc.apply(null, myArgs);  
  7. console.log(result); //output 10  
ES6 provides an easy way to the example above, via the spread operator. Again, let us see an example below. 
  1. function myFunc(arg1, arg2, arg3, arg4){  
  2.     return arg1 + arg2 + arg3 + arg4;  
  3. }  
  4.   
  5. let myArgs = [1,2,3,4];  
  6. let result = myFunc(...myArgs);  
  7. console.log(result); //output 10  
Isn't that nice? Now, to give you an idea of what's happening at runtime. Before JavaScript interpreter calls the myFunc function, it actually replaces ...myArgs with the 1,2,3,4 expression. You might be thinking, isn't the spread operator calls the apply() method. To answer that question, the JavaScript runtime doesn't call the apply method but the behavior is the same.
 

Other ways to use the spread operator

 
Making the array values as part of another array
 
Before ES6 there are probably a lot of ways to do this but today it's so simple. See the example below.
  1. let myFruits = ['apple''avocado''pineapple'];    
  2. let anotherFruits = ['chico', ...myFruits];    
  3. console.log(anotherFruits); //output [chico, apple, avocado, pineapple]     
Pushing the values of an array into another array
 
Before I tended to loop through an array and then push each item. In ES6 you no longer need to do that. Let us see an example below. 
  1. let dcSuperHeroes = ['Superman''Batman''Flash'];    
  2. let marvelSuperHeroes = ['Ironman'];    
  3. let superHeroes = [];    
  4. superHeroes.push(...dcSuperHeroes);     
  5. console.log(superHeroes); //["Superman", "Batman", "Flash"]    
  6. superHeroes.push(...marvelSuperHeroes);     
  7. console.log(superHeroes);//["Superman", "Batman", "Flash", "Ironman"]     
Spreading multiple arrays
 
If you want to spread an array into multiple different arrays, see the example below. 
  1. let dcSuperHeroes = ['Superman''Batman''Flash'];    
  2. let marvelSuperHeroes = ['Ironman'];    
  3. let japaneseSuperHeroes = ['Ultraman''Kamen Rider'];    
  4. let superHeroes = [...dcSuperHeroes, ...marvelSuperHeroes, ...['Son Goku''Gohan''Vegeta'], ...japaneseSuperHeroes];    
  5. console.log(superHeroes);   
  6. //output ["Superman", "Batman", "Flash", "Ironman", "Son Goku", "Gohan", "Vegeta", "Ultraman", "Kamen Rider"]     

The Rest parameter

 
The rest parameter is also represented by the "..." token. If you have seen a function having a last parameter prefixed with "..." it is called a rest parameter. Another thing, a rest parameter is an array type. Lastly, it contains the rest of the parameters of a function, when the number of arguments exceeded the number of named parameters.  
 
Before looking at the rest parameter example, let's try to see an example of how it is done without using the rest parameter.
  1. function myFunc(a,b){    
  2.     
  3.     var const_ArgsLen = 2,    
  4.         len = arguments.length;    
  5.         
  6.     if(len > const_ArgsLen){    
  7.     
  8.         var extraArguments = Array.prototype.slice.call(arguments, 2, len);    
  9.     
  10.         console.log(extraArguments); //output [3, 4, 5, 6]    
  11.     }    
  12. }    
  13. myFunc(1, 2,3, 4,5,6);     
As you can see, programmers before use the arguments objects of a function to retrieve the extra arguments that were passed to the function.
 
In ES6, this can be done in a much easier way. See the example below.
  1. function myFunc(a,b, ...args){    
  2.     console.log(args); //output [3, 4, 5, 6]    
  3.   }    
  4.   myFunc(1, 2,3, 4,5,6);     

Summary

 
In this blog post, we have discussed the spread and rest operator. I hope you have enjoyed this blog post, as much I have enjoyed writing and coding the examples. Until next time, happy programming.