When I'm writing utility functions, I'll often check for the existence of required arguments so I can return something other than the intended value if it's missing. I typically return null
or undefined
, and then I'll check that value where I'm using it.
For example:
function utility(arg1, arg2) {
if (!(arg a&& arg2)) {
return null;
}
// ... other code
}
var foo = utility(one, two);
if (foo) {
// .. so on
}
Because null
and undefined
and false
will return falsey when tested for truth, my questions are:
- Is there a benefit or best practice of returning one over the other? Or is it simply personal preference or maintaining consistency in a code base?
- Are there edge cases I'm not thinking of or haven't come across?