Given this function:
var test = function(param1, param2_maybe_not_set) {
var my_object = {};
// code here...
}
What's the best, in your opinion?
my_object.new_key = (param2_maybe_not_set === undefined) ? null : param2_maybe_not_set;
OR
my_object.new_key = (param2_maybe_not_set === void 0) ? null : param2_maybe_not_set;
OR
my_object.new_key = (typeof param2_maybe_not_set === 'undefined') ? null : param2_maybe_not_set;
Alternatively, would this shortened expression be correct?
my_object.new_key = param2_maybe_not_set || null;
All four methods work (in the NodeJS console at least). Also jsPerf doesn't show a big gap between any of these ()
Which one should be used, as a good practice?
Given this function:
var test = function(param1, param2_maybe_not_set) {
var my_object = {};
// code here...
}
What's the best, in your opinion?
my_object.new_key = (param2_maybe_not_set === undefined) ? null : param2_maybe_not_set;
OR
my_object.new_key = (param2_maybe_not_set === void 0) ? null : param2_maybe_not_set;
OR
my_object.new_key = (typeof param2_maybe_not_set === 'undefined') ? null : param2_maybe_not_set;
Alternatively, would this shortened expression be correct?
my_object.new_key = param2_maybe_not_set || null;
All four methods work (in the NodeJS console at least). Also jsPerf doesn't show a big gap between any of these (http://jsperf./typeof-performance/8)
Which one should be used, as a good practice?
Share Improve this question edited Apr 29, 2014 at 18:32 Samuel Bolduc asked Apr 29, 2014 at 18:23 Samuel BolducSamuel Bolduc 19.2k8 gold badges36 silver badges58 bronze badges 1- The last one is the most succinct and, I think, idiomatic in JavaScript. Alternatively, couldn't you just let the new property be undefined if the parameter is excluded? – net.uk.sweet Commented Apr 29, 2014 at 18:40
2 Answers
Reset to default 10They are not strictly equivalent, but can often be used interchangeably. Here are the major differences between them:
x === undefined
: this performs a strict-equality parison between the value andundefined
, meaning that only a actual value ofundefined
will betrue
, whereas similar values likenull
or0
will be false.In the case of a function call, this check does not differentiate between
f(a)
andf(a, undefined)
(in fact, none of the examples will; to differentiate, you'll have to look atarguments
).x === void 0
: this uses thevoid
keyword, which evaluates any expression and returnsundefined
. This was mostly done in the olden days to prevent surprises from people redefining the globalundefined
variable, but is not so useful nowadays (ECMAScript 5 mandates thatundefined
be read-only)typeof x === 'undefined'
: this uses thetypeof
keyword, which has a unique ability - namely, that the operand is unevaluated. This means that something liketypeof foobarbaz
returns'undefined'
even if no such variablefoobarbaz
exists at all. Contrast this withfoobarbaz === undefined
, which will throw a ReferenceError if the variable name has never been declared.x || null
: this is the simplest and probably most readable alternative. The||
operator is often used to "set defaults" on arguments, and can be chained likex || y || z || null
.In most cases, this is the idiomatic technique used. However, note that
||
performs implicit conversions, which means that any "falsy" values will trigger the next value (meaning that it can't differentiate betweenundefined
,false
,null
,0
,''
, andNaN
). So, if your function expects to receive falsy values as arguments, it may be more prudent to explicitly check forundefined
.
The option chosen to be an idiom in Javascript development to force a value for an unspecified argument is actually the last:
my_object.new_key = param2_maybe_not_set || null;
So this one should be preferrable since a lot of Javascript developers will immediately get its purpose.
Best.