Node.js/javascript helper for default value using typeof operator

I'm getting tired to have to always write this kind of code:

<% box_content = typeof box_content != 'undefined' ? box_content : ''%>
<% box_class = typeof box_class != 'undefined' ? box_class : 'box'%>

Does node.js or javascript (or a node module) have any helper for this? A simple function that return a default value or false if the default value isn't provided.

We need this so often, I don't understand why there is nothing in the node.js framework to help developpers to deal with that.

I'm thinking about adding this to my project but I would rather prefer having this in underscore.js as utility function for instance. What do you think?

Provided that box_content and box_class are declared, but their values may be undefined, then you can use || — JavaScript's curiously-powerful OR operator:

<% box_content = box_content || ''%>
<% box_class = box_class || 'box'%>

Unlike the logical OR operator in most languages, JavaScript's || returns the left-hand operand if its value is "truthy" or it's right-hand operand if it isn't. Any value that isn't "falsey" is truthy; the "falsey" values are 0, "", NaN, undefined, null, or (of course) false.

It's important to remember not to use this when a falsey value is valid; that can be a "gotcha." But quite frequently falsey values are not relevant, and so this is a very handy way to default things.


It seems from comments below that you don't care for the curiously-powerful || operator.

The answer, then, is no — but you can trivially create your own:

function foo(val, defVal) {
    return typeof val === "undefined" ? defVal : val;
}

<% box_content = foo(box_content, '')%>
<% box_class = foo(box_class, 'box')%>

Note that I didn't try to give foo a semantic name. Those are so subjective, you'll want your own anyway. :-)

Also note that this, too, requires that the variable be declared even if its value is undefined. If the variable may or may not be declared, the inline typeof is your only real option.

For global variables (only), this works whether the variables are declared or not:

function foo(varName, defValue) {
    var val;
    if (varName in global) {
        val = global[varName];
    }
    if (typeof val === "undefined") {
        val = defValue;
    }
    return val;
}

...where global is NodeJS's global variable that refers to the global object (like window on browsers).

Usage (note that the names of the variables are now strings):

<% box_content = foo('box_content', '')%>
<% box_class = foo('box_class', 'box')%>

But again, it's only useful for globals, which presumably you're avoiding using.

This is probably hacky, but here's a way to have a function that will let you input a nonexistant var without error;

function foo(inputVar,backupPlanVar){
   with(arguments.callee.caller){
      var evalVar;
      try{
         evalVar = this[inputVar];
      }catch(e){
         evalVar = backupPlanVar || undefined;
      }
      return evalVar;
   }
}

var abc = 123
foo('abc');
> 123
foo('nonExistantVariable');
> undefined
foo('nonExists', 456);
> 456

I would hesitate to actually use this, but maybe in some templating code?