Logical Assignment Operators in JS: Complete Guide
Logical assignment operators (&&=, ||=, ??=) combine a logical check with assignment in one step. Learn what each one does and when to use them.
JavaScript logical assignment operators combine a logical check with an assignment in one short expression. Instead of writing an if statement or a longer logical expression, you can use ||=, &&=, or ??= to conditionally update a variable.
They were introduced in ES2021 and are supported in all modern browsers and Node.js 15+.
The Three Logical Assignment Operators
| Operator | Name | Assigns if... | Equivalent long form |
|---|---|---|---|
| x ||= y | OR assignment | x is falsy | x || (x = y) |
| x &&= y | AND assignment | x is truthy | x && (x = y) |
| x ??= y | Nullish coalescing assignment | x is null or undefined | x ?? (x = y) |
Each one reads naturally: "OR-equals" means "assign if the left is falsy," "AND-equals" means "assign if the left is truthy," and "question-question-equals" means "assign if the left is nullish."
OR Assignment (||=)
OR assignment assigns a fallback value only when the left side is falsy. It is the most common of the three:
let username = "";
username ||= "Guest";
console.log(username); // "Guest" (empty string is falsy, so fallback assigned)The short form does not introduce new behavior, it just saves you from repeating the variable name. The long-form equivalent below makes the underlying logic explicit so you can see exactly what the shorthand replaces:
let username = "";
// These two lines do the same thing:
username ||= "Guest";
username || (username = "Guest");A practical use is setting default configuration values, where some settings might arrive as empty strings instead of being left out entirely:
const config = {
theme: "",
fontSize: 16
};
config.theme ||= "dark";
config.fontSize ||= 14;
console.log(config.theme); // "dark" (empty string replaced)
console.log(config.fontSize); // 16 (16 is truthy, kept)The ||= operator looks at all falsy values: false, 0, an empty string, null, undefined, and NaN.
AND Assignment (&&=)
AND assignment assigns a value only when the left side is already truthy. It is useful for conditional updates:
let message = "Hello";
message &&= message.toUpperCase();
console.log(message); // "HELLO" (message was truthy, so assignment runs)The assignment only fires when the current value is already truthy. If the left side is falsy instead, nothing happens and the variable keeps its original value:
let message = "";
message &&= message.toUpperCase();
console.log(message); // "" (empty string is falsy, assignment skipped)A practical pattern is updating a nested property only if the parent object already exists, which avoids a separate existence check before the update:
const user = { profile: { name: "Alice" } };
user.profile &&= { ...user.profile, verified: true };
console.log(user.profile);
// { name: "Alice", verified: true }If user.profile were null or undefined, the spread would never run, preventing a runtime error.
Nullish Coalescing Assignment (??=)
Nullish coalescing assignment assigns a fallback only when the left side is null or undefined, ignoring other falsy values like 0 and an empty string:
let score = 0;
score ??= 10;
console.log(score); // 0 (0 is not nullish, kept as-is)
let highScore = null;
highScore ??= 100;
console.log(highScore); // 100 (null triggers the fallback)This is the most precise of the three. Use it when 0, an empty string, and false are meaningful values that should not be overridden. For more on how ?? differs from ||, see the nullish coalescing guide.
Important: Left Side Evaluated Only Once
Logical assignment operators evaluate the left-hand side only once, not twice. This matters when the left side is more than a simple variable:
const scores = { alice: 85, bob: 0 };
scores.bob ||= 50; // Property access happens once
scores.bob || (scores.bob = 50); // Property access happens twiceBoth produce the same result, but OR assignment is slightly more efficient and definitely shorter. When the left side is a function call or a deep property access, evaluating once avoids unnecessary work.
When to Use Each
| Operator | Use when... | Example scenario |
|---|---|---|
| ||= | Any falsy value should trigger a default | Default theme, fallback username |
| &&= | Only update something that already exists | Transform a non-empty string, update a present object |
| ??= | Only null/undefined should trigger a default | Numeric defaults where 0 is valid, config overrides |
For simple default value assignments where you are not using the assignment-operator shorthand, the plain logical short-circuiting pattern with || or ?? may read more naturally. Choose the form that makes your intent clearest.
Rune AI
Key Insights
- x ||= y assigns y only if x is falsy (like a default value).
- x &&= y assigns y only if x is truthy (like a conditional update).
- x ??= y assigns y only if x is null or undefined (precise default).
- Each operator only evaluates the left side once, which matters for property access.
- Logical assignment operators were added in ES2021 and work in all modern environments.
Frequently Asked Questions
What does the &&= operator do?
Are logical assignment operators the same as regular assignment with a logical check?
When were logical assignment operators added to JavaScript?
Conclusion
Logical assignment operators (&&=, ||=, ??=) give you a concise way to conditionally assign values. ||= sets a fallback only when the variable is falsy. &&= sets a value only when the variable is already truthy. ??= sets a fallback only when the variable is null or undefined. They make common patterns shorter while still being readable once you learn the syntax.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.