Skip to content

Instantly share code, notes, and snippets.

@okjodom
Created May 14, 2018 09:03
Show Gist options
  • Save okjodom/7a6d0c1d80d01e406cab0de9f8bf92c9 to your computer and use it in GitHub Desktop.
Save okjodom/7a6d0c1d80d01e406cab0de9f8bf92c9 to your computer and use it in GitHub Desktop.
/* Because everybody loves a blue Subaru */
// Normal parameter defaults
function favoriteCar(car = 'Subaru', color = 'blue') {
return `Everybody loves a ${color} ${car}!`;
}
console.log(favoriteCar()); // Everyody loves a blue Subaru!
// Parameter defaults with object destructuring
function favoriteCar({car = 'Subaru', color = 'blue'} = {}) {
return `Everybody loves a ${color} ${car}!`;
}
console.log(favoriteCar()); // Everyody loves a blue Subaru!
// Parameter defaults with list destructuring
function favoriteCar([car = 'Subaru', color = 'blue'] = []) {
return `Everybody loves a ${color} ${car}!`;
}
// Skipping an index in list destructured defaults
console.log(favoriteCar([, 'white'])); // Everyody loves a white Subary!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment