Sub topic 11
Logical Assignment: ||=, &&= and ??=
This chapter assumes you already know how ||, && and ?? choose an operand, and that the right-hand side is skipped when the left side decides the result. Logical assignment operators combine those rules with assignment in a single step.
The Three Operators
Each operator checks the current value of its target. It assigns a new value only when the matching short-circuit operator would go on to evaluate its right-hand side.
| Operator | Assigns when the target is | Equivalent to | Column | Column | Column | Column |
|---|---|---|---|---|---|---|
| `a \ | \ | = b` | falsy | `a \ | \ | (a = b)` |
| `a &&= b` | truthy | `a && (a = b)` | ||||
| `a ??= b` | `null` or `undefined` | `a ?? (a = b)` |
The "Equivalent to" column is the exact meaning. a ||= b is not shorthand for a = a || b. In the second form, a is always assigned, even when it is assigned its own value. In the first form, no assignment happens at all when a is truthy.
Working Through an Example
const opts = { retries: 0, label: "" };
opts.retries ||= 3; // 0 is falsy, so retries becomes 3
opts.label ??= "default"; // "" is not nullish, so label stays ""
opts.timeout ??= 1000; // missing property is undefined, so timeout becomes 1000
let token = "abc";
token &&= token.toUpperCase(); // "abc" is truthy, so token becomes "ABC"
console.log(opts, token);
// { retries: 3, label: '', timeout: 1000 } ABCThe retries line shows the same trap as defaults written with ||. If 0 is a legitimate retry count, ||= overwrites it. Use ??= when only a missing value should be replaced.
&&= is the least common of the three. It is useful for transforming a value only when one exists, for example normalizing a string that might be null:
let input = null;
input &&= input.trim(); // input stays null, trim() is never called
let other = " hi ";
other &&= other.trim(); // other becomes "hi"
console.log(input, other); // null hiWhy "No Assignment" Matters
Skipping the assignment makes a visible difference when assignment has side effects, such as a setter or a proxy:
const store = {
_count: 5,
get count() { return this._count; },
set count(v) {
console.log("setter called with", v);
this._count = v;
},
};
store.count ||= 10; // nothing logged: 5 is truthy
store.count = store.count || 10; // logs "setter called with 5"The long form calls the setter on every run, even when it stores the value that was already there. That can trigger re-renders, change notifications or writes to storage. The logical assignment form skips the setter entirely.
The right-hand side is also short-circuited, so a costly default is computed only when it is needed:
const cache = {};
function buildIndex() {
console.log("building index");
return new Map();
}
cache.index ??= buildIndex(); // logs "building index"
cache.index ??= buildIndex(); // logs nothing; index already existsCommon Mistakes
- Using
||=for numbers, empty strings orfalsethat are valid values. Choose??=unless every falsy value really means "missing". - Expecting
&&=to set a value when the target is empty. It does the opposite and only replaces values that are already truthy. - Using these operators on an undeclared variable. Like any assignment, the target must be a valid reference. In strict mode,
missing ||= 1throws aReferenceErrorwhenmissingwas never declared.
Exercise
After this code runs, what are the values of a, b and c, and how many times is "called" logged? let a = 0; let b = null; let c = "x"; function f(v) { console.log("called"); return v; } a ??= f(1); b ||= f(2); c &&= f(3);
Show answer
a is 0, because 0 is not nullish and ??= skips the assignment. b is 2, because null is falsy. c is 3, because "x" is truthy. "called" is logged twice, once for f(2) and once for f(3). f(1) never runs.
Exercise
Rewrite settings.volume = settings.volume || 50 so that a volume of 0 is kept and the property is assigned only when it is missing.
Show answer
settings.volume ??= 50; This assigns only when volume is null or undefined, keeps 0, and does not reassign an existing value.