Nullish Coalescing

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.

nullish-coalescing.js
const username = null;
const displayName = username ?? "Guest";
console.log(displayName); // "Guest" because username is null

Here we ask ?? to pick between a real name and a fallback:

  • We set username to null, meaning we have no name to show.
  • username ?? "Guest" checks the left side first; since it is null, the operator returns the right side, "Guest".
  • We store that result in displayName and 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.

nullish-coalescing.js
const count = 0;
console.log(count || 10); // 10 → || treats 0 as missing
console.log(count ?? 10); // 0 → ?? keeps 0 because it is not null/undefined

This block runs the same value through both operators so we can see them diverge:

  • We set count to 0, a number that is falsy but still a real value.
  • count || 10 sees a falsy left side and falls back to 10, throwing away our 0.
  • count ?? 10 only falls back for null or undefined; since 0 is neither, it keeps 0.

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.

nullish-coalescing.js
const cartItems = 0;
const itemsToShow = cartItems ?? "No data";
console.log(itemsToShow); // 0 → correct, the cart really has 0 items

Here we protect a genuine 0 from being mistaken for missing data:

  • We set cartItems to 0, meaning the cart is real but empty.
  • cartItems ?? "No data" checks the left side; 0 is not null or undefined, so the operator keeps it.
  • We store and log that value, so 0 prints 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!

  1. Create a variable set to null and use ?? to give it a default.
  2. Set a variable to 0 and compare value || 10 with value ?? 10.
  3. Set a variable to an empty string and see how ?? keeps it.
  4. Decide for a username field whether ?? or || is the better choice.

🧩 What You’ve Learned

  • ?? returns the default only when the value is null or undefined
  • || falls back for any falsy value, including 0 and ""
  • ✅ Use ?? when 0, "", or false are 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.

Share & Connect