Last active
August 29, 2015 14:25
-
-
Save wilk/b774b4e395f08da7199a to your computer and use it in GitHub Desktop.
ES6 Parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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