Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. // Default Params
  2. function sum(x = 10, y = 20) {
  3. return x + y
  4. }
  5.  
  6. console.log(sum()) // 30
  7.  
  8. // Rest Params
  9. function sum(x, y, ...abc) { // ...abc = [30,40,50]
  10. let total = abc.reduce((a,b) => a + b) // 30+40+50 = 120
  11. return x + y + total // 10 + 20 + 120 = 150
  12. }
  13.  
  14. console.log(sum(10, 20, 30, 40, 50)) // 150
  15.  
  16. // Spread Operator
  17. let people = ['foo', 'bar', 'jar']
  18. let others = ['pippo', 'pluto', ...people]
  19. let numbers = [1,2,3,4,5]
  20.  
  21. console.log(sum(...numbers)) // 15
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement