I have a function which takes a parameter and performs some calculations with it. The parameter is a number. However, when the parameter is 0, js treats it as undefined. So if I have a check for it like:
if (param == 0) { ... }
This is always false because param==0 is false but param==undefined is true.
So I have to rewrite the code and have a special case for when param=0.
Example:
result= param*123*456 +789;
This has to be changed to:
if (param == undefined) {
result= 789;
} else {
result= param*123*456 +789;
}
return result;
This is repeating code. There must be a better way. What is the better way?
You can perform a
at the top of your function.
This will make sure that if
paramisundefinedit will become 0, so all your calculations will work as expected..