Skip to the solution
EasyFrontend6 min read

What is short-circuit evaluation?

505 views

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 0 β€” sideEffect() 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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 23 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What are the quirks of the typeof operator?

Continue