Skip to content

Instantly share code, notes, and snippets.

@wilk
Last active August 29, 2015 14:25
Show Gist options
  • Save wilk/b774b4e395f08da7199a to your computer and use it in GitHub Desktop.
Save wilk/b774b4e395f08da7199a to your computer and use it in GitHub Desktop.
ES6 Parameters
// Default Params
function sum(x = 10, y = 20) {
return x + y
}
console.log(sum()) // 30
// Rest Params
function sum(x, y, ...abc) { // ...abc = [30,40,50]
let total = abc.reduce((a,b) => a + b) // 30+40+50 = 120
return x + y + total // 10 + 20 + 120 = 150
}
console.log(sum(10, 20, 30, 40, 50)) // 150
// Spread Operator
let people = ['foo', 'bar', 'jar']
let others = ['pippo', 'pluto', ...people]
let numbers = [1,2,3,4,5]
console.log(sum(...numbers)) // 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment