What is Smart coding?
Smart coding is coding the logic in precise, consice and readable at the sametime maintainable manner.
I like to stree the above meaning in all the upcoming post under this series (Yes, you can expect more coming up) ^_^.
Selector Operator( || )
Yes, I like calling it selector operator as per its usuage.
1 | function greet(name) { |
We can achieve the same using the following approach too.
1 | function greet(name = 'guest') { |
The problem with the default argument value approach is that it will fail for empty string or null value.
1 | greet(null); // Hello null |
So, prefer the selector operator over the default argument value assignment.
Tip: We can also call a function or evaluate an expression to determine the default value like
name = name || determineDefaultName();\O/