logical assignment (&&=, ||=, ??=)
In JavaScript, the logical assignment operators (&&=, ||=, ??=) assign a new value to a variable or property only when its current value passes a logical test. When the test fails, the right side never runs and nothing is written.
The skipped write surprises people. The expression x ||= y means x || (x = y), not x = x || y, so a setter doesn’t run and a const binding doesn’t throw when the current value already passes. The operators were added in ES2021.
JavaScript Logical Assignment Syntax
Each operator takes an assignment target on the left and any expression on the right:
<target> &&= <expression>
<target> ||= <expression>
<target> ??= <expression>
The table shows when each operator assigns, starting from let count = 0;:
| Operator | Name | Assigns when the target is | Example | Result |
|---|---|---|---|---|
&&= |
Logical AND assignment | Truthy | count &&= 5 |
0 |
||= |
Logical OR assignment | Falsy | count ||= 5 |
5 |
??= |
Nullish coalescing assignment | null or undefined |
count ??= 5 |
0 |
Each expression evaluates to the target’s value after the operation, whether or not the assignment happened.
JavaScript Logical Assignment Examples
Filling in defaults on a settings object gives each operator a different job:
> const settings = { retries: 0, label: "", theme: " dark " };
undefined
> settings.retries ??= 3
0
> settings.label ||= "Untitled"
'Untitled'
> settings.theme &&= settings.theme.trim()
'dark'
> settings.timeout ??= 5000
5000
The ??= operator keeps the valid 0 retry count and fills in only the missing timeout. The ||= operator replaces the falsy empty string, and &&= trims theme because a value is already there.
Caching an expensive result shows short-circuit evaluation, because the right side runs only on the first call:
> const cache = {};
undefined
> function buildReport() {
… console.log("Building report...");
… return { rows: 120 };
… }
undefined
> cache.report ??= buildReport()
Building report...
{ rows: 120 }
> cache.report ??= buildReport()
{ rows: 120 }
On the second call, cache.report already holds an object, so buildReport() never runs.
JavaScript Logical Assignment Gotchas
These traps show up in real code:
- The
||=operator overwrites every falsy value, including a deliberate0,"", orfalse. - The target must be a variable or a plain property access. Optional chaining and destructuring patterns on the left side are a
SyntaxError. - A
consttarget throws aTypeErroronly when the assignment actually happens, so the error can hide until the value is missing.
JavaScript Logical Assignment Use Cases
Common uses include the following:
- Supplying a default for an option that’s
nullorundefined - Caching a computed value the first time it’s needed
- Replacing an empty string or other falsy value with a fallback
- Transforming a value only when it’s present
All three operators read the target once and write to it at most once.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026