Short Circuiting (part 2 “&&”)

Andrew Park
2 min readJun 5, 2021

In my previous blog, I wrote about Short Circuiting, using or (“||”) operator.

For a quick review, Short Circuiting using or (“||”) will return the value of first true operand, or the value of the last operand if all operands are false.

Short Circuiting using: or (“||”)

Short Circuiting using and (“&&”) operand will return:

  1. the value of first false operand: and (“&&”) operand will not continue if the current operand is false
  2. the value of the last operand if all operands are true
Short Circuiting using: and (“&&”)

For fist and last example, since the the value of all operands are true, the value of last operand is returned:

person.name && person.age && person.gender;      // "male"
person.name && ( () => person.weight = 150 )(); // 150

For all other examples, the value of first false operand is returned:

person.name && person.age && person.weight;      // undefined
person.name && null && person.gender; // null
0 && person.age && person.gender; // 0

I feel we shouldn’t over use Short Circuiting since it can make code hard to read. Use it only if your intension is quite obvious.

--

--