Skip to the solution
MediumFrontend6 min read

What are the logical assignment operators?

520 views

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

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: ??= preserves a legitimate 0 while ||= overwrites it, and all three logical assignment operators genuinely short-circuit

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 86 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 nextMedium

What is the difference between Object.freeze and const?

Continue