Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do `||` and `??` differ when choosing a fallback value?

Quick answer

|| uses the fallback whenever its left-hand value is falsy, so it replaces values such as 0, false, and "". ?? uses the fallback only when the left-hand value is null or undefined; it preserves 0, false, and "". Choose || when every falsy value should trigger a fallback. Choose ?? when only a nullish value should.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m choosing a default in JavaScript and keep seeing both || and ??. What’s the practical difference?

Geez

The difference is which left-hand values count as a reason to use the fallback. || returns the left value if it is truthy; otherwise, it returns the right value. That means a fallback can replace 0, false, or an empty string. [S1]

?? returns the fallback only when the left value is null or undefined. Other values, including 0 and an empty string, stay as they are. [S2]

A useful rule: use ?? when you mean “supply a value if this is missing.” Use || when you mean “supply a value if this is falsy.”

Follow-up question

Can you compare the results for a few values? I want to see exactly what changes.

Geez

Assuming the fallback is 42, here’s a side-by-side comparison:

Left value     With ||       With ??
0              0 || 42 → 42   0 ?? 42 → 0
""             "" || 42 → 42  "" ?? 42 → ""
null           null || 42 → 42 null ?? 42 → 42
undefined      undefined || 42 → 42  undefined ?? 42 → 42
false          false || 42 → 42 false ?? 42 → false

|| falls back for any falsy left-hand value; ?? falls back only for null or undefined. [S1][S2] The examples show what each operator does, but they don’t decide which result your application should want—that depends on what the value means in your task.

Follow-up question

How would I choose for a setting where zero is a meaningful value, but an unset value should get a default?

Geez

If 0 is meaningful and only an unset value should trigger the default, ?? is the better fit. For example, assuming retryLimit is a setting where zero means “allow no retries” and 3 is the intended default when the setting is absent:

const retryLimit = settings.retryLimit ?? 3;

If settings.retryLimit is 0, the result remains 0. If it is null or undefined, the result is 3. [S2]

By contrast, settings.retryLimit || 3 would also replace 0 with 3, because || falls back for falsy values. [S1] That could erase a deliberate zero. The meaning of zero in this example is an assumption; check your own setting’s intended meaning before choosing.

Follow-up question

What about a text label? An empty string may be intentional in one place but may mean ‘no label’ in another. Is there a quick way to decide?

Geez

Decide what an empty string represents in that specific case. If it is a valid, intentional value that should stay empty, use ?? so the fallback applies only to null or undefined. [S2] If an empty string should count as “no usable label” and be replaced, || matches that rule because it falls back for the empty string. [S1]

For example, assuming displayName is allowed to be deliberately blank in one feature, displayName ?? "Guest" preserves the blank value but uses "Guest" when the value is nullish. If your product instead treats a blank name as missing, displayName || "Guest" uses the fallback for that blank too. Write down the intended behavior first; the operator can’t decide what “missing” means for your application.

A practical checklist

  • Identify which left-hand values can occur, such as 0, false, "", null, or undefined.

  • Decide whether each value is meaningful data or should trigger a fallback.

  • Choose ?? if only null or undefined should trigger the fallback.

  • Choose || if every falsy value, including 0, false, and "", should trigger it.

  • Check the chosen expression against representative values.

A few more useful answers

Does `??` replace an empty string?

No. It preserves an empty string and uses the fallback only for null or undefined. [S2]

Does `||` replace zero?

Yes. 0 is falsy, so 0 || fallback returns the fallback. [S1]

Which operator should I use for a default setting?

Choose based on the setting’s meaning: use ?? if zero, false, or an empty string may be intentional; use || if those values should also activate the fallback.

Sources

References used for this guide.

  1. [S1] Logical OR (||) — MDN
    developer.mozilla.org · Checked
  2. [S2] Nullish coalescing operator (??) — MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work