最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handling undefined variables simple conditions in NodeJS - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 10

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 parison 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.

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.

发布评论

评论列表(0)

  1. 暂无评论