Skip to content

Instantly share code, notes, and snippets.

@iuliaL
Last active March 8, 2017 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iuliaL/df85b6f6eeda1e2732c1b9cf52bf2fd5 to your computer and use it in GitHub Desktop.
Save iuliaL/df85b6f6eeda1e2732c1b9cf52bf2fd5 to your computer and use it in GitHub Desktop.
Rest Parameters and Spread Operator
'use strict';
function myFunction (name, iceCreamFlavor) {
console.log(`${name} really likes ${iceCreamFlavor} ice cream.`)
}
const flavours = [ 'Banana', 'Lemon']
const lovedFlavours = [ "Straciatella", ...flavours] // this is the same as .concat()
const args = ['Iulia', lovedFlavours ];
myFunction(...args); //rest parameter
// Iulia really likes Straciatella,Banana,Lemon ice cream.
// more spread + rest
let arr = ['jesus', 'iulia', 'dan','hilary'];
const func = (...args) => { // args is a rest parameter
console.log(...args); // -> jesus iulia dan hilary
console.log(''+ args[0] + args[1]+ args[2]+args[3] ); //-> jesusiuliadanhilary
};
func(...arr); // here i spread the array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment