πŸš€Debugging Microservices & Distributed Systems
3 min read
intermediate

JavaScript's ??= Operator: Default Values Made Simple

A guide to using ??= in JavaScript to handle null and undefined values elegantly

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

JavaScript
// Old way (pre-2021)
if (user.name === null || user.name === undefined) {
user.name = 'Anonymous';
}
// Or using the nullish coalescing operator (??)
user.name = user.name ?? 'Anonymous';
// New way (ES2021 and later)
user.name ??= 'Anonymous';

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:

JavaScript
// Using if statement - verbose and repetitive
if (user.name === null || user.name === undefined) {
user.name = 'Anonymous';
}
// Using || operator - catches too much
user.name = user.name || 'Anonymous'; // Replaces '', 0, false too
// Using ternary - gets messy with longer expressions
user.name = user.name === null || user.name === undefined
? 'Anonymous'
: user.name;
// Using ??= - clean and precise
user.name ??= 'Anonymous';

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:

JavaScript
let score = 0;
score ??= 100; // Keeps 0
let tag = '';
tag ??= 'default'; // Keeps empty string
let active = false;
active ??= true; // Keeps false

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.


Related Articles

If you enjoyed this article, you might find these related pieces interesting as well.

Recommended Engineering Resources

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Imagine where you would be in two years if you actually took the time to learn every day. A little effort consistently adds up, shaping your skills, opening doors, and building the career you envision. Start now, and future you will thank you.


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.

Interested in a partnership? Shoot me an email at hi [at] trevorlasn.com with all relevant information.