Skip to content

Instantly share code, notes, and snippets.

@vibhanshuc
Last active February 9, 2017 20:08
Show Gist options
  • Save vibhanshuc/c83fc430f73118fa9799b8f7cded714b to your computer and use it in GitHub Desktop.
Save vibhanshuc/c83fc430f73118fa9799b8f7cded714b to your computer and use it in GitHub Desktop.
Default Arguments in ES2015
function calculateBill(total, tax = 0.15, tip = 0.05){
console.log(total + (total * tax) + (total * tip));
}
// usage
calculateBill(500); // 600
calculateBill(500, 0.2); // 625
calculateBill(500, 0.2, 0.3); // 750
calculateBill(500, undefined, 0.3); // 725
function calculateBill(total, {tax = 0.15, tip = 0.05}){
console.log(total + (total * tax) + (total * tip));
}
// usage
calculateBill(500, {}); // 600
calculateBill(500, {tax: 0.2}); // 625
calculateBill(500, {tip: 0.3}); // 750
calculateBill(500, {tax: 0.2, tip: 0.3}); // 725
function calculateBill(total, {tax = 0.15, tip = 0.05} = {}){
console.log(total + (total * tax) + (total * tip));
}
// usage
calculateBill(500); // 600
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment