Nullish Coalescing
Table of Contents + −
In the previous lesson, we used || to set default values. Now let’s learn a safer way to do that with the nullish coalescing operator.
❓ What is Nullish Coalescing?
The nullish coalescing operator ?? returns the right-hand value only when the left-hand value is null or undefined. Otherwise it returns the left-hand value.
It is mainly used to provide a default value.
const username = null;const displayName = username ?? "Guest";
console.log(displayName); // "Guest" because username is nullHere we ask ?? to pick between a real name and a fallback:
- We set
usernametonull, meaning we have no name to show. username ?? "Guest"checks the left side first; since it isnull, the operator returns the right side,"Guest".- We store that result in
displayNameand log it, so"Guest"prints.
🔍 ?? vs || (Why It Matters)
The OR operator || falls back to the default for any falsy value, including 0 and "". The ?? operator only falls back for null and undefined. This difference matters when 0 or an empty string is a valid value.
const count = 0;
console.log(count || 10); // 10 → || treats 0 as missingconsole.log(count ?? 10); // 0 → ?? keeps 0 because it is not null/undefinedThis block runs the same value through both operators so we can see them diverge:
- We set
countto0, a number that is falsy but still a real value. count || 10sees a falsy left side and falls back to10, throwing away our0.count ?? 10only falls back fornullorundefined; since0is neither, it keeps0.
So the difference is the trigger: || reacts to every falsy value, while ?? reacts only to null and undefined.
| Left value | value || "default" | value ?? "default" |
null | ”default" | "default” |
undefined | ”default" | "default” |
0 | ”default” | 0 |
"" | ”default” | "" |
When to use ??
Use ?? when 0, an empty string, or false are valid values you want to
keep. Use || only when any falsy value should fall back to the default.
🛒 A Practical Example
Imagine showing the number of items in a cart. A real count of 0 should display as 0, not the default.
const cartItems = 0;
const itemsToShow = cartItems ?? "No data";console.log(itemsToShow); // 0 → correct, the cart really has 0 itemsHere we protect a genuine 0 from being mistaken for missing data:
- We set
cartItemsto0, meaning the cart is real but empty. cartItems ?? "No data"checks the left side;0is notnullorundefined, so the operator keeps it.- We store and log that value, so
0prints instead of the"No data"fallback.
If you used || here, an empty cart would wrongly show “No data”.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
Using || when 0 is valid | A real 0 gets replaced by the default | Use ?? to keep 0 and "" |
Mixing ?? with && or || without parentheses | JavaScript throws a syntax error | Wrap them in parentheses to make the order clear |
Expecting ?? to catch all falsy values | It only catches null and undefined | Use || if you need to catch every falsy value |
🔧 Try It Yourself!
- Create a variable set to
nulland use??to give it a default. - Set a variable to
0and comparevalue || 10withvalue ?? 10. - Set a variable to an empty string and see how
??keeps it. - Decide for a username field whether
??or||is the better choice.
🧩 What You’ve Learned
- ✅
??returns the default only when the value isnullorundefined - ✅
||falls back for any falsy value, including0and"" - ✅ Use
??when0,"", orfalseare valid values to keep - ✅ Combining
??with&&or||requires parentheses
🚀 What’s Next?
Next we will learn how to safely read properties from objects that might not exist. Let’s continue to Optional Chaining.