DEV Community

Cover image for The JavaScript Bug Hiding in Your Default Values (?? vs ||)
Muiduzzaman Lipu for Refactorly

Posted on

The JavaScript Bug Hiding in Your Default Values (?? vs ||)

The user set the volume to 0. Your app set it to 50.

This bug is in a lot of JavaScript code, and it comes from one operator. Let's see why || breaks default values and how ?? fixes it. All outputs below are from Node.js 22.

Video version:

Default values with ||

function setup(options) {
  const volume   = options.volume   || 50;
  const name     = options.name     || "Guest";
  const autoplay = options.autoplay || true;
  return { volume, name, autoplay };
}

setup({ volume: 0, name: "", autoplay: false });
// { volume: 50, name: 'Guest', autoplay: true }  ❌
Enter fullscreen mode Exit fullscreen mode

The user turned the volume to zero, cleared the name and turned autoplay off, and every single choice was ignored.

Why: || replaces all falsy values

a || b returns b whenever a is falsy. 0, "", false and NaN are all falsy, but they're often real values a user chose.

a ?? b (nullish coalescing) returns b only when a is null or undefined, which means the value is actually missing.

| value | value \|\| "default" | value ?? "default" |
|---|---|---|
| 0 | "default" ❌ | 0 |
| "" | "default" ❌ | "" |
| false | "default" ❌ | false |
| NaN | "default" | NaN |
| null | "default" | "default" |
| undefined | "default" | "default" |

The fix: ??

const volume   = opts.volume   ?? 50;
const name     = opts.name     ?? "Guest";
const autoplay = opts.autoplay ?? true;
Enter fullscreen mode Exit fullscreen mode
setup({ volume: 0, name: "", autoplay: false });
// { volume: 0, name: '', autoplay: false }  ✅

setup({});
// { volume: 50, name: 'Guest', autoplay: true }  ✅ defaults still work
Enter fullscreen mode Exit fullscreen mode

The same trap: ||= vs ??=

const config = { retries: 0 };
config.retries ||= 3;   // → 3  ❌ overwrote a real 0

const config2 = { retries: 0 };
config2.retries ??= 3;  // → 0  ✅

const empty = {};
empty.retries ??= 3;    // → 3  ✅ only fills in missing values
Enter fullscreen mode Exit fullscreen mode

?.: safe access, pairs well with ??

const user = { profile: null };

user.settings.theme
// TypeError: Cannot read properties of undefined (reading 'theme')

user.settings?.theme                  // undefined
user.profile?.email ?? "no email"     // 'no email'

const api = {};
api.getUser?.()                       // undefined: called only if it exists
Enter fullscreen mode Exit fullscreen mode

Don't sprinkle ?. everywhere, though. If a value should always exist, a crash points you straight at the bug, while ?. just hides it.

When to use which

  • ?? gives a default only for null / undefined. This is what you want for most defaults.
  • || gives a default for any falsy value. It's rarely what you want for options and settings.
  • ??= sets a value only if it's missing.
  • ?. gives safe access, returning undefined instead of crashing.
  • Gotcha: you can't mix || and ?? without parentheses:
a || b ?? c     // SyntaxError: Unexpected token '??'
(a || b) ?? c   // ✅
Enter fullscreen mode Exit fullscreen mode

Go search your code for || 0, || "", || true and || 50-style defaults. You might find this bug. 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)