Skip to solution
easyFrontend

What is short-circuit evaluation?

505 views
01

Understand the problem

Question presented to candidate: "If I write someCondition && doSomethingExpensive(), and someCondition is false, does doSomethingExpensive() actually get called? Walk me through exactly why or why not."

What a strong answer should cover:

  • 📌 Interview term: short-circuit evaluation&& and || evaluate their left-hand side first, and stop entirely — never evaluating the right-hand side at all — as soon as the overall result is already determined.
  • 📌 Interview term: the direct, verified answer to the prompt — verified directly with a real call counter: false && sideEffect() genuinely left the counter at 0sideEffect() was never actually called, not just its return value ignored. true && sideEffect() genuinely called it (counter became 1).
  • 📌 Interview term: ||'s mirrored rule — verified directly: true || sideEffect() genuinely left the counter at 0 (short-circuited on the first truthy value), while false || sideEffect() genuinely called it.
  • A precise answer names the real, common practical use: a guard pattern like user && user.name — verified directly, this genuinely avoids a real TypeError that user.name alone would throw when user is null, because the short-circuit never even attempts to evaluate user.name at all.
  • 📌 Interview term: the classic real pitfall — verified directly: count || 10 used as a "default value" pattern genuinely returns 10 even when count is the legitimately valid value 0, because 0 is falsy; the real, correct fix for exactly this case is the nullish coalescing operator ??, which verified directly correctly preserved 0.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering whether the expensive function call actually happens (with real proof it is skipped entirely, not just its result discarded) is the strong signal.

Code / implementation expected: Yes — a real call-counter proof that the right-hand side is never even invoked is the clearest, most convincing demonstration.

short-circuit
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every call-count claim below was actually run in Node, using a real counter, not an assumpt

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof with a call counter: the right-hand side of && and || is never evaluated when short-circuited, plus the classic 0-default pitfall
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 23 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track