||= &&= ??= operators Playground

Three copies of a discount form on the counter: ||=, &&= and ??= side by side with x = x || y, and a counting setter and fallback show who really writes. An…

Why it matters

Filling defaults (??=), setting up an expensive cache only once (??= skips the work when it is there), updating only what exists (&&=). Picking ||= over ??= is a classic bug: a valid 0 or empty text is silently overwritten.

The key idea

Short-circuiting assignment: when the test fails, nothing is written

Common mistakes

Try it in this playground