February 1, 20222 min read


It always feels euphoric when I can write a block of code to a single line.

Take for example the following code:

if (confirm) {

Nothing fancy. It's just an if statement that calls a function when the value of confirm is truthy. This snippet is probably common in handling user action in confirm dialogs.

The code can be written in one line as follows:

if (confirm) { saveUserSelection(); }

Just kidding hehe...


confirm && saveUserSelection();

This is called "short-circuit evaluation". With short-circuit evalutation, the right side expression of the AND operator is evaluated only if the left side is truthy. Otherwise, it just returns the left side expression.

I really like this approach for it takes less effort in reading and writing. Not writing an if statement means that I can save extra spaces, indentation and characters such as {, }, ( and ). And saving characters means that I will only have to read minimal code and still be able to understand what the code is for.

This was once a topic on our team code review and I thought it might be good to write about it.

It might take some time to get used to this approach, especially in working on a team. But you'll get a hang of it by using it more often.

Another example could be:

loggedInUser && showProfileScreen() && updateUserAvatar();


Now playing :Not playing any music.