Member-only story
Logical Assignment Operators in JavaScript — The Tiny Feature That Saves You Time
Write cleaner logic with JavaScript’s ||=, &&=, and ??= — here’s how and when to use them.
Have you ever written code like this?
if (!config.port) {
config.port = 3000;
}Or like this?
if (user.isLoggedIn) {
user.token = getToken();
}If yes, I have good news — JavaScript has a much cleaner way to handle these patterns thanks to logical assignment operators, introduced in ES2021. These small additions to the language can eliminate repetitive code and make your logic much more expressive.
Let’s explore what they are, how they work, and where you should (and shouldn’t) use them.
What Are Logical Assignment Operators?
Logical assignment operators combine:
- A logical operator (
||,&&, or??) - With the assignment operator (
=)
That gives us:
||=– Logical OR assignment (assign if falsy)&&=– Logical AND assignment (assign if truthy)??=– Nullish coalescing assignment (assign if nullish)









