Tip 19 Maximize Efficiency with Short Circuiting

In this tip, you’ll learn to reduce conditionals to the smallest possible expression with short circuiting.

You’ve been simplifying conditional expressions a lot in the last few tips. But there’s one more level of simplification you can use: short circuiting.

As the name implies, the goal of short circuiting is to bypass information checks by placing the most relevant information first.

Consider the following ternary, which would fit in well with the discussion from the previous chapter.

 function​ getIconPath(icon) {
 const​ path = icon.path ? icon.path : ​'uploads/default.png'​;
 return​ ​`https://assets.foo.com/​${path}​`​;
 }

The goal here is fairly ...

Get Simplifying JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.