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.

4 min read

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

OperatorNameAssigns if...Equivalent long form
x ||= yOR assignmentx is falsyx || (x = y)
x &&= yAND assignmentx is truthyx && (x = y)
x ??= yNullish coalescing assignmentx is null or undefinedx ?? (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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
const scores = { alice: 85, bob: 0 };
 
scores.bob ||= 50;   // Property access happens once
scores.bob || (scores.bob = 50); // Property access happens twice

Both 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

OperatorUse when...Example scenario
||=Any falsy value should trigger a defaultDefault theme, fallback username
&&=Only update something that already existsTransform a non-empty string, update a present object
??=Only null/undefined should trigger a defaultNumeric 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

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.
RunePowered by Rune AI

Frequently Asked Questions

What does the &&= operator do?

x &&= y assigns y to x only if x is truthy. If x is falsy, x stays unchanged. It is shorthand for x && (x = y).

Are logical assignment operators the same as regular assignment with a logical check?

Yes, but they are shorter and only evaluate the left side once. x ||= y is equivalent to x || (x = y), not x = x || y. This matters when the left side is a property access or function call.

When were logical assignment operators added to JavaScript?

They were introduced in ES2021 (ES12). All modern browsers and Node.js versions from 15+ support them. They do not work in Internet Explorer.

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.