ES6: Configure default parameters for your functions
#ES6 #ECMAScript6 #Javascript #WebDevelopment
ECMAScript-6 came with the default option to configure default parameters
Consider the following block of code:
const schedule = (status = "Busy") => "Current status: " + status;
// firstOutput
console.log(schedule("Available"));
// what will firstOutput log to the console?
// secondOutput
console.log(schedule());
// what will secondOutput log to the console?
Based on the above example, the firstOutput will log to the console:
"Current status: Available"
This is due to the specification of an argument ("Available" in this case) for our "status" parameter.
In the secondOutput, this result will be logged to the console:
"Current status: Busy"
Here in the secondOutput, the parameter name will receive a default value, ("Busy" in this case). This is as a result of not specifying an argument (or a value) for the parameter (We can say at this point that the parameter is undefined), hence resulting to the default.
// See here. A value or argument is NOT defined.
// Hence defaults to the default value
console.log(greeting());
// But in this case,
// A value or argument was defined ("Available" in this case)
console.log(greeting(Available));`
The good thing about this "default parameters" feature in ES6, is that it encourages flexibility when writing functions.
If you have need for more clarifications, you can drop your questions in the comment section. You can share your knowledge as well for others to learn.
Thanks for reading through this article. I hope you found it helpful in your quest to understanding javascript.
You can reach out to me on my social handle Twitter