Guarding in JavaScript

After looking at JavaScript programming sessions by Douglas Crockford (here), I couldn't stop but admire JavaScript. There are many many impressive things in JavaScript but one of the eye-catchy thing was guarding operator in JavaScript.

The && operator in JavaScript can be acted in two ways. The normal 'and' operator which is used in determining the conditional logic results. The other lesser known feature of JavaScript is the guard operator. In following code -

 
/* The if is checking if specs is not undefined, false, null,
"" (empty string) or 0 (numeric zero) as these all values
are falsy conditions in JavaScript */
 
if(specs)
     width = specs.width;
 
// To avoid the if condition you can simply do like -
 
width = specs && specs.width;
 
/* This makes sure that specs is not falsy and they
returns the second operand which is convenient
way to write null check */
 

The same is with || operator too. If the first operand is falsy then it just returns the second operand instead. Consider following code:

 
name = specs.name || "Name not set";
/* Use the specs.name, but if name doesn't have a value, use default
value instead. */
 

Very simple and very efficient!

This entry was posted in Developer, Programming. Bookmark the permalink.
  • http://siddharthdawara.blogspot.com Sid

    Ruby does the same thing with both && and ||

  • http://pkailasa.multiply.com prakash

    Another important aspect of these operators is the “short-circuiting” behavior. The second operand is evaluated only if the first operand can not determine the value of the expression. In the case of ‘||’ operator, for example, the second operand isn’t evaluated if the first operand is true.

    Most dynamic languages whose ancestry can be traced back to C (which was the first to “spell” these operators like this) support this type of logical operator behaviour.

    AFAIK, Perl was the the first language that borrowed this “short-circuit” behavior from C and and improved it. Whereas in C the expression in the RHS evaluates to either 0 or 1, in most dynamic languages, the RHS evaluates to one of the two operands. This improvement, as you have noticed, gives rise to very concise and efficient, yet readable, code.

  • http://InternetDuctTape.com engtech

    Check out Reg’s andand gem for ruby as well.

    It lets you do the equivalent of && in method chains.

    http://andand.rubyforge.org/

  • Pingback: Weekend Reader - twitter, blogging, usability, webdesign, bsg « // Internet Duct Tape

  • http://www.kushalm.com kushal
  • http://haineault.com h3

    Yeah it’s one of the many things I like in JavaScript (http://www.haineault.com/blog/44/).

    It’s also useful to combine it with the “||” operator, like this:

    width = specs && specs.width || default.width;

  • Emmett

    width = specs && specs.width;
    is not exactly the same thing as
    if(specs.width) width = specs.width;
    The first will always overwrite the value of width; the second will only overwrite width if specs.width exists.