Skip to solution
mediumFrontend

What are the logical assignment operators?

520 views
01

Understand the problem

Question presented to candidate: "If you write config.retries ??= 3 and config.retries is already 0, does it get overwritten? What if you'd used ||= instead — same answer?"

What a strong answer should cover:

  • 📌 Interview term: ??=, ||=, &&= — the logical assignment operators, each conditionally assigning the right-hand side to a variable/property based on the CURRENT value, combining a logical check with assignment in one step.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: x ??= value genuinely only assigns when x is currently null/undefined — a real, legitimate 0 genuinely survives ??= untouched. x ||= value genuinely assigns for ANY falsy value — the identical real 0 genuinely gets overwritten by ||=, a real, sharp, different answer to the prompt's second question.
  • 📌 Interview term: &&= — the mirror case: x &&= value genuinely only assigns when x is currently truthy — verified directly, a null value genuinely stays null, since &&= never assigns to something already falsy.
  • 📌 Interview term: genuine short-circuiting — verified directly with a real call counter: when the condition for assignment is not met, the right-hand side expression is genuinely never evaluated at all — not just its result discarded, the exact same short-circuiting guarantee this bank's own dedicated short-circuit-evaluation question covers for plain &&/||.
  • A precise answer names the real, practical use case directly matching the prompt: config.retries ??= 3 is the correct, idiomatic way to supply a default ONLY for a genuinely missing config value, without accidentally clobbering a deliberately-set 0, false, or "".

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering both halves of the prompt's own scenario (??= vs. ||= on a real 0) is the strong signal.

Code / implementation expected: Yes — the direct side-by-side ??=-vs-||=-on-a-real-0 comparison is the clearest, most convincing demonstration.

logical-assignment
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 assignment and short-circuit claim below was actually run in Node.

1. Why This Ev

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: ??= preserves a legitimate 0 while ||= overwrites it, and all three logical assignment operators genuinely short-circuit
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 86 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track