← Back to list

JavaScript Fallback Values Explained: OR (||) vs Nullish (??)

Learn the real difference between JavaScript’s OR (||) and nullish coalescing (??) operators. Understand when to use each, how to handle .

Vetriselvan Panneerselvam · 2025-11-14 02:56 · 95 claps · 1.7 min read paywalled
#jstips #nullish-coalescing #operator-or #js #javascript
Open on Medium ↗
Wiki topics: 🌐 · Web Development

JavaScript Fallback Values Explained: OR (||) vs Nullish

JavaScript Fallback Values Explained: OR (||) vs Nullish

JavaScript Fallback Values Explained: OR (||) vs Nullish (??)

H ey Devs 👋

JavaScript has a funny way of confusing developers — mostly because it gives us so many ways to do the same thing. I got bored of writing if conditions everywhere, so I jumped into using the OR operator (||) and the nullish coalescing operator (??).

Most of the time, I was using these operators without fully understanding their true purpose. (Not really — but it makes the story more dramatic, hehe. )

Not a Medium member? You can read the full article for free by clicking here 🔗

When I was a fresher, I saw these operators used everywhere. So let’s properly understand what they actually do.

1. Logical OR operator (||)

The logical OR operator returns the first truthy value, or the last value if nothing is truthy.

Falsy values in JavaScript include:

false, 0, "", null, undefined, NaN

Example

console.log("" || "default");     // "default"
console.log(0  || 42);            // 42
console.log(null || "fallback");  // "fallback"

Because 0 and "" are falsy, || replaces them.

Use || when you want a fallback for any falsy value.

2. Nullish coalescing operator (??)

The nullish coalescing operator returns the right-hand value only when the left-hand value is null or undefined.

It does not treat 0, false, or "" as missing.

Example

console.log("" ?? "default");      // ""  (not replaced)
console.log(0 ?? 42);              // 0
console.log(false ?? true);        // false
console.log(null ?? "fallback");   // "fallback"

👉 Use ?? when you want a fallback ONLY if the value is missing (null or undefined).

When to Use Which?

| Use Case                                        | Operator | Why                                      |   | 
| ----------------------------------------------- | -------- | ---------------------------------------- | - |  
| Provide fallback when *any* falsy value occurs  | `||`     | Catches empty strings, 0, false, etc.    | ` |
| Provide fallback **only when value is missing** | `??`     | Does not treat 0, false, or "" as errors |   | 
| Form inputs, optional settings                  | `??`     | User might enter `0` or empty string     |   |
| Old-style fallback for booleans or numbers      | `|| `    | Works for general falsey checks          |   |

Thanks for reading! If this was helpful, consider clapping 👏 and following for more js tips. Got questions or suggestions? Drop them in the comments below!

✍️ Author: **Vetriselvan Panneerselvam**

👨‍💻 Frontend Developer | 💡 Code Enthusiast | 📚 Lifelong Learner | ✍️ Tech Blogger | 🌍 Freelance Developer


메타데이터
post_id
3fe73c58dbe3
slug
javascript-fallback-values-explained-or-vs-nullish-3fe73c58dbe3
url
https://medium.com/@vetriselvan_11/javascript-fallback-values-explained-or-vs-nullish-3fe73c58dbe3
canonical_url
https://medium.com/@vetriselvan_11/javascript-fallback-values-explained-or-vs-nullish-3fe73c58dbe3
author_url
https://medium.com/@vetriselvan_11
status
ok
fetched_at
2026-07-14 19:59:56