Shaving Bytes on JavaScript Conditionals

By  on  

Whenever you work with JavaScript code, it's as though there's always a shorter way to code something.  You thought that a code set was basic until you found out that something was basic...er.  One of those code shortcuts can be found with conditions, specifically short if clauses.

A typical short if clause would look something like this:

if(callback) {
	callback();
}

Oddly enough this conditional can be made shorter:

callback && callback();

The && is less code than the if(){}; of course only by a few characters but does the same job. You could argue that readability suffers but that's up to individual developers.

Recent Features

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Incredible Demos

  • By
    Chris Coyier’s Favorite CodePen Demos IV

    Did you know you can triple-heart things on CodePen? We’ve had that little not-so-hidden feature forever. You can click that little heart button on any Pen (or Project, Collection, or Post) on CodePen to show the creator a little love, but you can click it again...

  • By
    jQuery Comment Preview

    I released a MooTools comment preview script yesterday and got numerous requests for a jQuery version. Ask and you shall receive! I'll use the exact same CSS and HTML as yesterday. The XHTML The CSS The jQuery JavaScript On the keypress and blur events, we validate and...

Discussion

  1. It’s worth noting that JS minifiers like Google’s Closure Compiler will do this for you, so the first option is probably better so you get the readability without sacrificing performance. The Closure Compiler outputs it as this:

    callback&&callback();

    http://closure-compiler.appspot.com/home

  2. Herbut

    and also jshint might shout about the shorter version (depending on the settings of course).

  3. It’s bad practice though because the code is hard to maintain, debug and extend. I could write a whole blog on why doing this is bad. I see zero benefits.

  4. Agree with comments above. I recently realized that there is no benefits of having expressions in my code so changed jshint settings and now it disallows to use them.

    IMO the expression below is pretty readable and it also takes one line:
    if (callback) callback();

  5. Agree with the “bad practice” comments.

    Sometimes you seem to post stuff just for the sake of it, or to impress beginners.

    • I appreciate your honesty but impressing people isn’t something that entertains me.

  6. Ana

    What if I also need to have an else branch?

    • There’s only “if”, I suppose. Otherwise it’s something like:

      callback ? callback() : otherThing();
      
  7. While I agree with people’s comments on code readability, I still appreciate posts like this.
    I’ve come across the ‘callback && callback();’ syntax before and had to look up wtf was going on. Had I read this post earlier, I would’ve known :)

  8. Sean

    @Dan i agree with you, posts like this are handy so you understand when you come across it in a project. Sadly, this is clearly lost on a couple of the previous commenters who already know everything there is to know.

  9. What setting will make jsHint happy?

  10. Readability is important, but for those who like to hyper-optimize their code, this is a great tip.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!