Shaving Bytes on JavaScript Conditionals
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.
![6 Things You Didn’t Know About Firefox OS]()
Firefox OS is all over the tech news and for good reason: Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript. Firefox OS has been rapidly improving...
![Responsive Images: The Ultimate Guide]()
Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...
![HTML5 Placeholder Styling with CSS]()
Last week I showed you how you could style selected text with CSS. I've searched for more interesting CSS style properties and found another: INPUT
placeholder styling. Let me show you how to style placeholder text within INPUT
elements with some unique CSS code.
The CSS
Firefox...
![Using Opacity to Show Focus with jQuery]()
A few days back I debuted a sweet article that made use of MooTools JavaScript and opacity to show focus on a specified element. Here's how to accomplish that feat using jQuery.
The jQuery JavaScript
There you have it. Opacity is a very simple but effective...
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
and also jshint might shout about the shorter version (depending on the settings of course).
No one writes code for JSHint :)
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.
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();
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.
What if I also need to have an else branch?
There’s only “if”, I suppose. Otherwise it’s something like:
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 :)
@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.
I’m not worried about them.
What setting will make jsHint happy?
Code is better than anything, 2 JsPerf :
– http://jsperf.com/if-statement-verses-and-operator
– http://jsperf.com/ternary-vs-and-or-vs-if-else
Readability is important, but for those who like to hyper-optimize their code, this is a great tip.