What Is Meant by Short-Circuit Evaluation in JavaScript "&&" Expression?

In JavaScript, short-circuit evaluation in the logical AND operator refers to the fact that if the first part of a logical AND expression (e.g. "a" in a && b) evaluates to false, then the second part of the expression is not evaluated. This means that if the left operand is a falsy value, then the operand on the right is never evaluated.

In cases where the expression short-circuit evaluates to false, the operand that evaluated to false is the one that is returned. To illustrate this, consider for example, the following:

console.log(false && true); // false

console.log(1 && null && 'never reaches here'); // null

console.log([].length && true); // 0

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.