1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

JS - Logical AND Operator Used as If Statement?

Discussion in 'Off Topic' started by James, Sep 2, 2010.

  1. James

    James Well-Known Member

    Reading an article on NetTuts+ by Jeffrey Way made me intrigued by the concept of this. I haven't ran any testing to check if an if() statement or this method runs faster... but it's interesting nonetheless.

    For users who don't want to read the article, Jeffrey basically states that with JS when using the AND operator, the second statement only runs if the first statement is true.
    var a=5;
    if((a===5) && (b===10)){
    b===10 will only run if a=5. With this in mind, other methods become available to us, such as:
    !document.getElementById('contents') && createElem('div', 'contents', 'hello world');  

    This will check if an element exists with an ID of contents. If it doesn't, it will create a DIV with an ID of contents.

    A more real-life example was also given:
    !window.jQuery && document.write('<script src="localjQuery.js"><\/script>');  
    which downloads a local copy of jQuery if the copy was not retrievable from the CDN for whatever reason.

    I found it an interesting read and decided to share it :p
  2. Shadab

    Shadab Well-Known Member

    /slightly OT

    It's true for C, C++, Java and PHP as well.
    (can't say about other languages, I'm only familiar with these 4).

    This is the short-circuit evaluation of expressions. ie, the evaluation of a logical expression stops when you have sufficient information to decide the outcome of the whole expression. Thus when using logical OR, if the first sub-expression evaluates to true the second sub-expression is not evaluated because no matter what it evaluates to, the result will always be true.

    Similarly in a logical AND, if the first sub-expression evaluates to false, the result will be false no matter what the other sub-expressions evaluate to. :)
  3. James

    James Well-Known Member

    Thanks for the explanation Shadab :p
    Do you know if this method is faster than a standard if() statement? I must remember to run some testing at some point.
  4. Luke F

    Luke F Well-Known Member

    I think you'll find this is common knowledge ;)
  5. Shadab

    Shadab Well-Known Member

    I'm not a Javascript guy, so I don't know and I'm unable to write tests to benchmark it in JS. :(

    But in PHP, yes it's slightly faster. We're looking at an improvement of just about 2000 ms over 10,000,000 iterations; which is 0.2 x 10^-6 second per iteration. That's micro optimization and obviously not something we should go crazy about.

    In the conclusion section of the article over at NetTuts+ they say "It certainly makes for slightly less readable code", and rightly so!

    What may be obvious or common knowledge for some of us, may not be so for others who are less technical. :)
    Forsaken likes this.

Share This Page