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

Tech executive with over a decade of experience programming and building products. Building 0xinsider.com, ceodumps.com and whiteheap.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

Intermediate variables name each step of complex logic, and early returns handle failing cases first. A JavaScript authentication check, rewritten both ways.

Aug 30, 2024
Read article
JavaScript
6 min read

Peaks.js is a client-side JavaScript component to display and interact with audio waveforms in the browser

Oct 22, 2019
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
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

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

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

Aug 23, 2024
Read article
JavaScript
5 min read

How to handle exact decimal calculations in JavaScript when floating-point precision isn't good enough

Nov 16, 2024
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
3 min read

Copy and paste text, images, and files using the new navigator.clipboard API

Dec 7, 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.