/ JavaScript

Shorthand Javascript "if this do that" blocks.

Often times when writing JavaScript I constantly run into blocks like below:


Recently I've seen a different implementation of the same logic which looks like...

shouldDoThis && doThing();

The way the && operator works is it's executed from left to right, only continuing if it hasn't hit a falsy value. So if 'shouldDoThis' is not true, it doesn't continue on to execute the function 'doThing()'.

So in the code below, it would only execute foo() if all 3 required items evaluate as true.

required1 && required2 && required3 && foo()

What are your thoughts on this?

Corey Snyder

Corey Snyder

Senior Front-End Engineer for Aver Inc.. I have independently developed & released multiple video-games. I play Ice Hockey, I race FPV Drones, and I love my Subaru WRX STI.

Read More