Operators & Short-Circuit Evaluation

Difficulty: Beginner

Question

What is short-circuit evaluation in JavaScript, and how do the &&, || and ?? operators differ?

Answer

Short-circuit evaluation means a logical operator stops evaluating as soon as the result is known, and returns one of its operands rather than a boolean. a && b returns a if it is falsy, otherwise b. a || b returns a if it is truthy, otherwise b. a ?? b returns a unless it is null or undefined, which makes it the safe choice for default values.

The operators return operands, not booleans

This is the part people miss. '' || 'Guest' evaluates to 'Guest', and 'Rahul' && 'Admin' evaluates to 'Admin'. The result is only a boolean if the operands are booleans. JavaScript checks truthiness, and only these values are falsy: false, 0, -0, 0n, '', null, undefined and NaN. Everything else, including '0', [] and {}, is truthy.

Because evaluation stops early, the right-hand side may never run. isLoggedIn && fetchProfile() only calls fetchProfile when isLoggedIn is truthy.

|| vs ??: the default value bug

|| treats every falsy value as "missing". That breaks when 0, '' or false are legitimate values:

Value of a`a \\10`a ?? 10
0100
''10''
false10false
null1010
undefined1010
555

If a user sets a discount to 0 or a quantity to 0, quantity || 1 silently changes it to 1. quantity ?? 1 keeps the zero. Use ?? (ES2020) whenever you mean "if not provided".

Related operators: ?. and logical assignment

Gotchas worth knowing

&&, || and ?? return operands, not booleans. Compare 0 || "hi", 0 ?? "hi" and "" && "hi" under Tweak.
  1. ToBoolean(left): The left operand is tested for truthiness.
  2. || returns the first truthy operand: 0 || "hi" gives "hi" because 0 is falsy.
  3. && returns the first falsy operand: "" && "hi" gives "" and never evaluates the right side.
  4. ?? only skips null and undefined: 0 ?? "hi" gives 0, so valid zeros and empty strings survive.

Code examples

Operators return operands

console.log('' || 'Guest');        // Guest
console.log('Rahul' || 'Guest');   // Rahul
console.log('Rahul' && 'Admin');   // Admin
console.log(0 && 'never reached'); // 0
console.log(null ?? 'default');    // default

// right side is skipped when the result is known
let calls = 0;
const track = () => { calls++; return true; };
false && track();
true || track();
console.log('calls:', calls);      // calls: 0

The operators return whichever operand decided the result, and the right side never runs when the left side already settles it.

Defaults: || vs ?? vs ??=

function createOrder(opts) {
  const qtyOr = opts.quantity || 1;     // bug: 0 becomes 1
  const qtyNullish = opts.quantity ?? 1; // keeps 0
  return { qtyOr, qtyNullish };
}
console.log(createOrder({ quantity: 0 }));
console.log(createOrder({}));

const config = { retries: 0, timeoutMs: undefined };
config.retries ??= 3;      // stays 0
config.timeoutMs ??= 5000; // filled in
console.log(config);

?? and ??= only treat null and undefined as missing, so a deliberate 0 survives.

Optional chaining with a fallback

const order = { id: 'SWG-42', customer: null };

console.log(order.customer?.address?.city ?? 'Unknown city');
console.log(order.items?.length ?? 0);
console.log(order.notify?.());   // calls only if notify exists

// (order.customer || {}).name  -> old pattern
// order.customer?.name         -> modern pattern

?. stops the chain at the first null or undefined and returns undefined, and ?? supplies the fallback.

Key points

Common follow-up questions

Interview tips

Concepts covered

Short-Circuit Evaluation, Logical OR, Nullish Coalescing, Optional Chaining, Logical Assignment

More JavaScript Interview questions