The Lab · Bugs
I Swapped || for ?? and Found 33 Wrong Defaults
Merging 12 settings records with || instead of ?? changed 33 of 96 fields; 11 of 12 records broke.
Each line jumps to its section
- Merging 12 saved settings records with OR instead of nullish coalescing changed 33 of 96 fields silently
- 11 of the 12 records had at least one field quietly overwritten by a default it didn't need
- quantity: 0 became quantity: 1 in four separate records, the single most common wrong field
- Switching the merge function to nullish coalescing fixed every one of the 33 mismatches, nothing else changed
I'd read the warning about || and falsy values a dozen times and still wasn't sure how much it actually mattered in practice, so I built twelve small "saved settings" records, the kind a form or a URL query string hands you, and merged each one against a defaults object two different ways.
One merge used ||. The other used ??. Same records, same defaults, same loop.
The Setup I Actually Ran
Each of the twelve records had eight fields: quantity, a discount code, a notifications flag, a theme, a retry count, a page size, a label, and a rating. I didn't write these to be a trick. I wrote them the way real saved settings look, where at least one field on almost every record is legitimately zero, empty, or false. A cart with quantity 0 before checkout. A discount code field that's blank because nobody used one. A rating of 0 because a customer hasn't rated anything yet.
function mergeWithOr(settings, defaults) {
const out = {};
for (const key of Object.keys(defaults)) {
out[key] = settings[key] || defaults[key];
}
return out;
}
function mergeWithNullish(settings, defaults) {
const out = {};
for (const key of Object.keys(defaults)) {
out[key] = settings[key] ?? defaults[key];
}
return out;
}
Run both functions over the same twelve records, against the same defaults object, and diff every field.
I wasn't trying to invent a worst case either. The eight fields were picked to look like a normal settings object on any small store or tool: a quantity, a discount code, a notifications toggle, a UI theme, a retry count, a page size, a label, and a star rating. Nothing exotic, nothing contrived to make || look bad on purpose. If anything, I expected most records to come through clean and one or two edge cases to show up, the way edge cases usually do.
What 96 Comparisons Turned Up
Ninety-six total fields across the batch, eight fields times twelve records. Thirty-three of them came out different depending on which operator did the merging. That's more than a third.
Eleven of the twelve records had at least one field that || silently replaced with a default the record never needed. Only one record in the whole batch came through clean, and that was mostly luck. It happened to not set anything to zero, empty, or false that run.
The field that broke most often was rating, wrong in six of twelve records, every time because a rating of 0 isn't "no rating entered," it's a real rating of zero stars. Right behind it was retries and notifications, each wrong five times. A retries: 0 record is telling you it already gave up retrying. A notifications: false record is someone who explicitly opted out. Both got overwritten back to the default the instant || touched them.
Here's the one I keep coming back to:
input: { quantity: 0, discountCode: 'SAVE5', ... }
|| : quantity = 1
?? : quantity = 0
A shopper who set their cart quantity to zero, maybe clearing it before checkout, gets quantity 1 back from a settings merge that never should have touched that field at all. Nobody files a bug report for that. They just see an extra item they didn't add.
Four separate records hit that exact quantity bug, which made it the single most common failure in the whole batch, ahead even of the rating field by a hair once I counted carefully. A quantity field quietly resetting to its default is also the one most likely to go unnoticed for weeks, since the number it lands on, usually 1, still looks completely plausible sitting in a cart or an order record. Nothing crashes. Nothing logs an error. The record just quietly stops reflecting what someone actually chose.
Why This Only Shows Up Later
The reason this bug survives code review so often is that it needs a specific kind of input to trigger, and most manual testing never produces that input by accident. Type a quantity of 2 into a test form and || and ?? agree completely. Every demo, every quick smoke test, every "looks good to me" review passes, because nobody manually tests a settings merge with a quantity of zero unless they already suspect something's wrong.
It's the same shape as the Promise.all bug I measured a while back, or the sort bug that only shows up once a list crosses ten items: the function works right up until one specific, realistic input shows up, and then it fails in a way that looks like something else entirely. A missing discount, a quantity that's off by one, a notification that fires after someone turned it off. Each one reads like a different bug in a different part of the codebase, and all 33 of mine traced back to the same two characters.
?? only falls back when the left side is exactly null or undefined. || falls back on every falsy value: 0, '', false, NaN, all of it. If a field can ever legitimately hold one of those values, || is the wrong tool for filling in a missing one, full stop.
Code review doesn't catch this reliably either, and I don't think that's a knock on whoever's reviewing. A reviewer reading settings[key] || defaults[key] sees a completely ordinary fallback pattern, the same shape used correctly in a thousand other places in the same codebase where the field genuinely can't be a meaningful zero. Telling the two cases apart means knowing, field by field, which ones can legitimately hold a falsy value on purpose, and that's domain knowledge a diff doesn't carry. The bug isn't in how the line reads. It's in what the data underneath it is allowed to be.
A linter can flag the pattern, but it can't tell you whether it's wrong. ESLint's no-mixed-operators and similar rules will nag about combining || and ?? in the same expression without parentheses, and a type checker can tell you a field is typed as number, but neither one knows that your particular number field is allowed to be 0 on purpose. That judgment call still has to come from whoever wrote the schema, which in a one-person shop is always the same person writing the merge function. The tooling narrows where to look. It doesn't make the decision for me.
The Fix and What Changed
I changed one line in the merge function, swapping the operator and nothing else, and reran the comparison. All 33 mismatches disappeared. Zero fields differed between the old || output and the corrected ?? output across all twelve records once both used the same operator.
That's not a coincidence, it's the whole point. ?? isn't a stricter version of ||, it's answering a different question. || asks "is this value falsy." ?? asks "does this value exist." For a settings merge, a form default, or anything reading a saved record back, "does this value exist" is almost always the question I actually meant to ask.
I went back through older merge functions I'd written before I knew to look for this, searching for bare || next to a defaults or settings object. I found three. Every one of them had a field that could plausibly be 0 or false on purpose: a discount percentage, a boolean flag, a sort order starting at index 0. I fixed all three the same way, swapped the operator, confirmed nothing else changed.
Two of the three weren't even in a settings merge function by name. One was parsing a URL query string into an options object, falling back to a default page number whenever url.searchParams.get('page') || 1 came back falsy, which happens for the real page 0 some paginated APIs use for the first page. The other read environment variables with the same pattern, process.env.RETRY_LIMIT || 3, which quietly ignores anyone who deliberately sets that variable to 0 to disable retries entirely. Neither of those looked anything like the settings-object example at the top of this piece, and both had the exact same bug hiding in them.
I don't reach for ?? by habit yet, not everywhere || shows up. For genuinely boolean gates, like "use this value if it's not undefined and also not an empty string," a short explicit check still reads clearer than chaining operators. But for merging a record against defaults, where some fields can honestly be zero, I don't write || anymore. Twelve test records were enough to show me why.
Bottom Line
Thirty-three wrong fields out of ninety-six, from a batch of twelve records that weren't trying to break anything, just trying to look like real saved settings. That's the actual size of this bug when 0, '', and false are legitimate values in your data, which they usually are once a product has been live for more than a week.
The fix costs nothing. Swap || for ?? in a merge function and nothing about the surrounding code has to change, because the two operators only disagree on inputs most manual testing never reaches. If your settings, your form state, or your Shopify app's saved preferences can ever hold a real 0, an empty string, or a false, that's the merge function worth opening today, not the one you'll get to eventually.
I'm not auditing every file for this tonight. I'm starting with the places most likely to carry a real falsy value on purpose: anything that reads a saved record, a query string, or an environment variable back and fills in a gap. Those three turned up every mismatch I found. Everywhere else can wait for the next time I'm already in that file for another reason.
This article contains affiliate links. If you sign up through them, I may earn a small commission at no extra cost to you. (Ad)