Logical Assignment Operators in JavaScript Explained
The operators ||=, &&= and ??= assign only when the left side is falsy, truthy or nullish, and skip the write otherwise. See defaults, caches and setters.

The pattern if (options.timeout === undefined) options.timeout = 5000 is so common that the language gave it a short form. options.timeout ??= 5000 does the same in one line, and its two relatives, ||= and &&=, cover the neighboring cases. They are small operators with one property that matters more than the saving in keystrokes: they skip the assignment when it is not needed, which differs from the longhand a = a || b in ways that show up with setters, frozen objects and expensive right-hand sides. This post explains each operator, the short-circuit rule, three practical patterns and the pitfalls, with output from Node.js 24. They are the assignment forms of the operators in the post on nullish coalescing and logical OR, and they work with the values in the list of falsy values.
The three operators
Each one tests the left side and assigns only if the test says so.
let a = null;
let b = 0;
let c = "text";
a ??= "default";
b ||= 10;
c &&= c.toUpperCase();
console.log(a, b, c);
let empty = "";
let zero = 0;
let flag = false;
empty ??= "kept";
zero ??= 99;
flag ??= true;
console.log(JSON.stringify(empty), zero, flag);
let blank = "";
blank ||= "filled";
console.log(blank);default 10 TEXT
"" 0 false
filled??= filled the null and left the empty string, the zero and false alone, because none of those is nullish. ||= replaced the zero and the empty string, because both are falsy. &&= ran its right side only because c was truthy. Pick the operator by the question you are asking about the left side: missing, falsy or present.
| Operator | Assigns when the left side is | Equivalent | ||||
|---|---|---|---|---|---|---|
a ??= b | null or undefined | a ?? (a = b) | ||||
| `a \ | \ | = b` | Falsy | `a \ | \ | (a = b)` |
a &&= b | Truthy | a && (a = b) |
It is not the same as a = a || b
The table's last column is the point. The longhand a = a || b always performs an assignment, even if it writes back the same value. The logical form writes only when it needs to. The difference is invisible for a plain variable and visible for a property with a setter.
const log = [];
const tracked = {
_value: "set",
get value() {
return this._value;
},
set value(next) {
log.push("setter ran with " + next);
this._value = next;
},
};
tracked.value = tracked.value || "fallback";
tracked.value ||= "fallback";
tracked.value ??= "fallback";
console.log(log);
tracked._value = null;
tracked.value ??= "fallback";
console.log(log);
console.log(tracked.value);[ 'setter ran with set' ]
[ 'setter ran with set', 'setter ran with fallback' ]
fallbackThe longhand called the setter even though the value was already set, and the two logical forms did not. Only when the stored value became null did ??= call it. That matters when a setter validates, logs, notifies a subscriber or triggers a re-render, because an unnecessary write can cause an extra render or an infinite loop in a reactive system.
It works on frozen objects until it has to write
Because no write happens when the condition is not met, a logical assignment on a read-only property is fine as long as the property is already set.
const config = Object.freeze({ retries: 3 });
config.retries ??= 5;
console.log(config.retries);
try {
config.timeout ??= 1000;
} catch (error) {
console.log(error.name + ": " + error.message);
}3
TypeError: Cannot add property timeout, object is not extensibleThe first line wrote nothing, so the freeze was not violated. The second needed to add a property, and an ES module is strict, so it threw. A sloppy script would have ignored the write. This is a useful property for code that "ensures a default exists" on an object that may or may not be frozen.
Pattern: filling in option defaults
The most common use is normalizing an options object at the top of a function.
function startServer(options = {}) {
options.port ??= 3000;
options.host ??= "localhost";
options.logging ??= { level: "info" };
options.logging.level ??= "info";
options.retries ??= 0;
return options;
}
console.log(startServer());
console.log(startServer({ port: 0, logging: {} }));
console.log(startServer({ retries: 2, host: "0.0.0.0" }));{
port: 3000,
host: 'localhost',
logging: { level: 'info' },
retries: 0
}
{ port: 0, logging: { level: 'info' }, host: 'localhost', retries: 0 }
{ retries: 2, host: '0.0.0.0', port: 3000, logging: { level: 'info' } }A port of 0 was respected, which ||= would have replaced. The nested logging object was filled and then its own level was filled, in two steps. Note that this function edits the object it was given, and the caller sees the changes, a mutation concern covered in the post on primitives versus objects. If the options object is shared or frozen, copy it first: const settings = { ...options }.
Pattern: a keyed cache
??= expresses memoization in one line, with the computation running only on a miss.
const cache = new Map();
const results = {};
let computed = 0;
function square(n) {
computed += 1;
return n * n;
}
function lookup(n) {
return (results[n] ??= square(n));
}
console.log(lookup(4), lookup(4), lookup(5));
console.log(computed);
function mapLookup(n) {
if (!cache.has(n)) cache.set(n, square(n));
return cache.get(n);
}
console.log(mapLookup(6), mapLookup(6));
console.log(computed);16 16 25
2
36 36
3The object cache computed each distinct input once. The expression also returns the value, since an assignment is an expression, so return (results[n] ??= square(n)) both stores and returns. It does not work for a Map, because a Map does not use property assignment, so the second function shows the longer form. It also treats a cached null or undefined as a miss, since those are what ??= tests for. If your function can legitimately return undefined, use has(), as the Map version does.
Pattern: updating only when present
&&= modifies a value that might be absent and leaves a missing one alone.
const user = { name: " Ada ", nickname: null };
user.name &&= user.name.trim();
user.nickname &&= user.nickname.trim();
user.email &&= user.email.toLowerCase();
console.log(user);{ name: 'Ada', nickname: null }The name was trimmed. The nickname was null, so nothing happened, and no TypeError was raised for calling .trim() on null. The email did not exist, so the property was not created, which is the difference from user.email = user.email?.toLowerCase(), which would have written undefined into a new key. If you want the property to exist afterward, use ??= for that purpose.
The pitfalls
The first is ||= with legitimate falsy values. It overwrites zero, the empty string and false, so a user who set a volume to 0 or cleared a field gets the default back. Use ??= unless blank is meant to be treated as missing.
const settings = { volume: 0, title: "", muted: false };
const withOr = { ...settings };
withOr.volume ||= 50;
withOr.title ||= "Untitled";
withOr.muted ||= true;
const withNullish = { ...settings };
withNullish.volume ??= 50;
withNullish.title ??= "Untitled";
withNullish.muted ??= true;
console.log(withOr);
console.log(withNullish);{ volume: 50, title: 'Untitled', muted: true }
{ volume: 0, title: '', muted: false }The second pitfall is the left side being evaluated once. For a.b.c ??= x, the object a.b is evaluated a single time, which is an advantage over repeating the path, and a call on the left, like getObject().x ??= 1, calls getObject once. The third is that the operators cannot be applied to a const binding that would need a write: const x = null; x ??= 1; throws TypeError: Assignment to constant variable., because a write is needed. And they work on identifiers, properties and elements, and not on destructuring patterns.
Reading and reviewing them
A review question for each logical assignment is the same one as for the plain operators: what can the left side hold, and is replacing it correct for each value? For ??= the answer is almost always yes. For ||= the answer deserves a comment, because a reader cannot tell whether the author meant "empty is missing" or forgot that zero is falsy. For &&= the useful check is whether the property should exist afterward, since the operator never creates one.
Adopting them in an existing codebase is low risk when the replacement is a one-for-one rewrite of an existing if or ternary, and the tests cover a nullish and a falsy left side. Codemods and eslint --fix with logical-assignment-operators in the operator-assignment family can do the first pass, and the diff is small enough to read in full.
Where this advice stops
If a default should apply on every call and not mutate its input, build a new object with spread, { ...defaults, ...options }, which also handles nested data better than a series of ??= lines. If a value needs validation and not only a default, validate explicitly. And for a cache that can grow without bound, add eviction, since ??= stores everything.
The operators are in ES2021 and run on Node.js 15 and later, and any browser from 2020 onward. A transpiler rewrites them into the longhand for older targets.
The syntax is defined in the specification under assignment operators, and MDN has a page for each of ??=, ||= and &&=.


