Course contentsShow
← JavaScript
Lesson 217 of 2,7236. Booleans and ConditionalsFree lesson

Short-Circuit Evaluation with &&

See how && stops evaluating as soon as it finds a falsy value, preventing side effects.

Short-Circuit Evaluation with &&

You'll learn: How the && operator stops evaluating as soon as it encounters a falsy value, which can prevent code from running.

The Core Idea

When JavaScript evaluates an && expression, it works from left to right and stops (short-circuits) the moment it finds a falsy value. Once a falsy value is found, JavaScript knows the entire expression must be false, so it doesn't bother checking the rest.

Think of it like checking into a concert: you need both a ticket AND valid ID. If the bouncer sees you don't have a ticket, they don't even bother asking for your ID—you're not getting in either way.

Why This Matters

This behavior has practical implications when your expressions include function calls or operations with side effects:

let count = 0;

function increment() {
  count++;
  return true;
}

// Short-circuits: increment never runs
false && increment();
console.log(count); // 0

// Does not short-circuit: increment runs
true && increment();
console.log(count); // 1

In the first case, false && immediately evaluates to false, so increment() never executes. The second case reaches increment() because the left side is truthy.

Common Real-World Usage

This pattern appears frequently when checking conditions before performing actions:

let user = null;

// Only logs if user exists (truthy)
user && console.log(user.name);

user = { name: "Alice" };
user && console.log(user.name); // "Alice"

Key Takeaway: The && operator short-circuits and stops evaluating as soon as it finds a falsy value, preventing any subsequent code in that expression from running—a useful behavior for conditional logic and avoiding errors.