Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emfluenceindia/1086bb9b0cac1446f7a83e34917896d3 to your computer and use it in GitHub Desktop.
Save emfluenceindia/1086bb9b0cac1446f7a83e34917896d3 to your computer and use it in GitHub Desktop.
ES6 - Example of handling named parameter and multiple return value using object destructuring
/**
The following code snippet shows how to use named parameters
by using the help of Object Destructuring.
Function emiCalculator() takes one argument whcih is a destructured object with
multiple entries, of which two have default values set (in ES6 we can set default values for parameters)
When calling the function we would pass three values i.e. rawPrice and downPayment and emiCount (although it has a
default value set) and won't pass any value for 'interestPerAnum'.
For destructured parameters we have pass their values like { parameterName: {properyName: value} }
and access them the similar way, i.e., parameterName.value
The function also handes multiple value return, i.e. a JavaScript object
which later we destructure and show the desired output.
*/
const emiCalculator = ( { rawPrice, downPayment, interestPerAnum = 10, emiCount = 8 } ) => {
const interestApplied = rawPrice.value * ( interestPerAnum / 100 );
const finalPrice = rawPrice.value + interestApplied;
let remainingAmount = ( finalPrice - downPayment.value );
let extendedWarrantyCost = 0;
const emiAmount = remainingAmount / emiCount.value;
/**
Build multiple values bundled in a JavaScript object.
*/
const objEmiDetail = {
rawProductPrice: rawPrice.value,
interestCharged: interestApplied,
finalProductPrice: finalPrice,
downPayment: downPayment.value,
remainingAmount: remainingAmount,
noOfEmi: emiCount.value,
emiAmount: emiAmount
}
// return the above object
return objEmiDetail;
}
/**
Call function emiCalculator() by immediately destructuring the object objEmiDetail
which is returned by the function.
*** Overriding default value of emiCount and skipping default parameter interestPerAnum ***
I have passed three parameters and skipped 'interestPerAnum', which is not the ending parameter.
Meaning, with named parameters, we are not restricted to pass values for all like passing either null or undefined
for paramters sitting in between others with a default value.
Here, the order of the named parameters is rawPrice, downPayment, interestPerAnum, emiCount and we can easily skip the interestPerAnum.
The default value of emiCount (8) will get overridden by the one we pass (12).
*/
const {
rawProductPrice,
interestCharged,
finalProductPrice,
downPayment,
remainingAmount,
noOfEmi,
emiAmount} = emiCalculator(
{
rawPrice: { value: 24000 },
downPayment: { value: 8000 },
emiCount: { value: 12 }
}
);
/**
Outputting values using template literals
*/
console.log(`
Base price: ${rawProductPrice.toFixed(2)}
Interest: ${interestCharged.toFixed(2)}
Purchase Price: ${finalProductPrice.toFixed(2)}
Down Payment: ${downPayment.toFixed(2)}
Remaining: ${remainingAmount.toFixed(2)}
EMI (Remaining / ${noOfEmi}): ${emiAmount.toFixed(2)}`
);
/**
Output:
Base price: 24000.00
Interest: 2400.00
Purchase Price: 26400.00
Down Payment: 8000.00
Remaining: 18400.00
EMI (Remaining / 12): 1533.33
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment