...
/The Spread Operator and Rest Parameters
The Spread Operator and Rest Parameters
We'll cover the following...
Two of my favorite new features in ES6 are the Spread Operator and Rest Parameters. These two features really allow us to create powerful operations that might have required more work, and added more confusion than needed. Let’s first take a look at Rest Parameters.
Rest Parameters
From time to time you might want to write a function that would take an unknown number of arguments. JavaScript has the ability to do this through the arguments
keyword. Take a sum()
function, say it takes only two numbers
function sum(a,b) {return a + b;}
Maybe we want this sum()
to take any number of arguments? We can tell how many arguments have been passed to a function by using the arguments
keyword.
function sum() {console.log(arguments);}let total = sum(1,3,4,6); //[1,3,4,6]
This is great: it looks like it returns an array for us to work with. We can go ahead and use the ...