2

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.com/typeof-performance/8)

Which one should be used, as a good practice?

Samuel Bolduc
  • 18,163
  • 7
  • 34
  • 55
  • 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 Apr 29 '14 at 18:40

2 Answers2

9

They are not strictly equivalent, but can often be used interchangeably. Here are the major differences between them:

  • x === undefined: this performs a strict-equality comparison between the value and undefined, meaning that only a actual value of undefined will be true, whereas similar values like null or 0 will be false.

    In the case of a function call, this check does not differentiate between f(a) and f(a, undefined) (in fact, none of the examples will; to differentiate, you'll have to look at arguments).

  • x === void 0: this uses the void keyword, which evaluates any expression and returns undefined. This was mostly done in the olden days to prevent surprises from people redefining the global undefined variable, but is not so useful nowadays (ECMAScript 5 mandates that undefined be read-only)

  • typeof x === 'undefined': this uses the typeof keyword, which has a unique ability - namely, that the operand is unevaluated. This means that something like typeof foobarbaz returns 'undefined' even if no such variable foobarbaz exists at all. Contrast this with foobarbaz === 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 like x || 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 between undefined, false, null, 0, '', and NaN). So, if your function expects to receive falsy values as arguments, it may be more prudent to explicitly check for undefined.

Community
  • 1
  • 1
voithos
  • 68,482
  • 12
  • 101
  • 116
1

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.

E. Valencia
  • 447
  • 2
  • 6