$ thought | blog

Place to serialize my thoughts…

Guarding in JavaScript

with 7 comments

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!

Written by Sachin

April 25th, 2008 at 8:04 am

7 Responses to 'Guarding in JavaScript'

Subscribe to comments with RSS or TrackBack to 'Guarding in JavaScript'.

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

    Sid

    25 Apr 08 at 10:03 am

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

    prakash

    25 Apr 08 at 6:53 pm

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

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

    http://andand.rubyforge.org/

    engtech

    25 Apr 08 at 9:41 pm

  4. [...] [JAVASCRIPT] Guarding in JavaScript, thoughtworker.in, via:lazycoder.com [...]

  5. kushal

    4 May 08 at 6:26 am

  6. 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;

    h3

    4 May 08 at 1:06 pm

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

    Emmett

    4 May 08 at 10:55 pm

Leave a Reply