Skip to content

Instantly share code, notes, and snippets.

@gil00pita
Forked from gisderdube/default.js
Last active February 8, 2019 23:15
Show Gist options
  • Save gil00pita/205c711a84c6031b4428323e028f642d to your computer and use it in GitHub Desktop.
Save gil00pita/205c711a84c6031b4428323e028f642d to your computer and use it in GitHub Desktop.
https://levelup.gitconnected.com/9-tricks-for-kickass-javascript-developers-in-2019-eb01dd3def2a ### Destructuring & default values Let’s return to our previous example where we do the following: ``` const result = axios.get(`https://ironhack-pok
function calculate({operands = [1, 2], type = 'addition'} = {}) {
return operands.reduce((acc, val) => {
switch(type) {
case 'addition':
return acc + val
case 'subtraction':
return acc - val
case 'multiplication':
return acc * val
case 'division':
return acc / val
}
}, ['addition', 'subtraction'].includes(type) ? 0 : 1)
}
console.log(calculate()) // 3
console.log(calculate({type: 'division'})) // 0.5
console.log(calculate({operands: [2, 3, 4], type: 'multiplication'})) // 24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment