The ||
is just the regular old or operator. It comes in handy when the value is expected to not be falsey. But, if values like 0
, false
, or null
are valid and expected, you need to take a different approach.
== null
To check to see if a non-null value was passed, use == null
. This will return true
when null
or undefined
are passed in:
function optionalArguments (a, b) {
a = a == null ? "nothing" : a;
b = b == null ? "nothing" : b;
...
}
In most cases, this is the best approach for implementing optional parameters. It allows the caller to pass null
when the default value is desired. It's particularly useful when a caller wants to pass a value for the second argument, but use the default for the first. Eg, optionalArguments(null, 22)
=== undefined
If null is a valid and expected value, compare as above using undefined
and the ===
operator. Make sure that you are using a valid value of undefined
for your comparison. It is possible for a script to say var undefined = 0
, causing endless headaches for you. You can always do === void 0
to test for undefined
.
arguments.length
What if I call your function like this?
optionalArguments("something", void 0);
In this case, I did pass a value, but that value is undefined
. There may be times when you truly want to detect whether an argument was passed in or not. In this case, you need to check the arguments.length
:
function optionalArguments (a, b) {
a = arguments.length > 0 ? a : "nothing";
b = arguments.length > 1 ? b : "nothing";
...
}