Nullish Coalescing Assignment (??=) in JavaScript

x ??= y assigns y only when x is null or undefined, so 0, false and empty strings stay. How it differs from || and where it saves code.

The nullish coalescing assignment operator ??= is relatively new to JavaScript. It was officially added in ECMAScript 2021 (ES12) as part of the “Logical Assignment Operators” proposal.

Think of ??= as a smart guardian for your variables. It only assigns a new value if the current one is null or undefined

When you write something like user.name ??= 'Anonymous', the operator first checks if user.name is null or undefined.

If it is null or undefined, only then does it assign 'Anonymous' — If user.name already has any other value - even an empty string or 0 - it stays untouched.

Why ??= Beats The Alternatives

Before ??=, we had several ways to handle default values, but each had its drawbacks. Compare these approaches:

The ??= operator gives us precision we didn’t have before. It only triggers when we truly have no value, making it perfect for cases where zero, empty strings, or false are valid data:

This precision helps prevent bugs that can occur when using broader checks. When building user interfaces or handling form data, you often want to preserve falsy values rather than replacing them with defaults.


Trevor I. Lasn

Trevor I. Lasn

Building whiteheap.com and 0xinsider.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

JavaScript
4 min read

How the Suspense component manages async dependencies and improves loading states in Vue apps

Aug 23, 2024
Read article
JavaScript
3 min read

Float16Array stores 16-bit floating point numbers in half the memory of a Float32Array. How it works, when to use it and which browsers support it.

Apr 14, 2025
Read article
JavaScript
7 min read

A WeakRef points to an object without stopping the garbage collector from freeing it. When that helps, how deref() works, and cleanup with FinalizationRegistry.

Jan 7, 2025
Read article
JavaScript
4 min read

Stop writing manual duration formatting code. Instead, leverage the new powerful Intl.DurationFormat API for internationalized time displays

Mar 13, 2025
Read article
JavaScript
9 min read

Deep dive into JavaScript Symbols - what they are, why they matter, and how to use them effectively

Nov 15, 2024
Read article
JavaScript
7 min read

JavaScript has eight falsy values: false, 0, -0, 0n, an empty string, null, undefined and NaN. Empty arrays and objects are truthy. The edge cases, explained.

Oct 27, 2024
Read article
JavaScript
6 min read

Closures are essential for creating functions that maintain state, without relying on global variables.

Sep 6, 2024
Read article
JavaScript
7 min read

AggregateError holds several errors in one, the way Promise.any() reports that every promise rejected. How to throw and read it, plus the new SuppressedError.

Sep 2, 2024
Read article
JavaScript
7 min read

|| returns the first truthy value, && the first falsy one, and ?? falls back only on null or undefined. When to use each, and the cases that trip people up.

Oct 26, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/javascript-nullish-coalescing-assignment-operator. It was written by a human and polished using grammar tools for clarity.