Confessions and Confusions of Ashok M A. Personal and Professional Blog.

Ashok Mannolu Arunachalam, Smart codingJS

Smart coding: short-circuit expression in JavaScript

Short circuit expressions are simply cute and compact.^_^

It can be used as a fail-fast mechanism. You want to do specific actions if and only if a case is true.

Yes, you heard that right! To simplify the if cases in general. Take a look at the approach below:

const isOnline =
if (isOnline) {
const data = fetchData()

Now we can simplify this using the short-circuit expression as follows:

const data = && fetchData()

Isn't she a beauty! \O/

Yes, the return value of the last expression is assigned to the data .