
A settings screen saves a retry count of zero, but the next run uses three retries. The value may have survived storage perfectly. The bug can be the defaulting expression: retries || 3 treats zero as a reason to choose the fallback. I would inspect that expression before blaming the database or the form.
Missing and falsy are different policies
The logical OR operator, ||, returns the right operand when the left operand is falsy. That includes false, zero, an empty string, NaN, null and undefined. It does not necessarily return a Boolean; it returns an operand's value.
The nullish coalescing operator, ??, uses the fallback only for null or undefined. It preserves zero, false and an empty string. Neither rule is universally better. Choose the one that matches what the field means.
const input = {
retries: 0,
notifications: false,
caption: ''
};
console.log(input.retries || 3); // 3
console.log(input.retries ?? 3); // 0
console.log(input.notifications ?? true); // false
console.log(input.caption ?? 'Untitled'); // ''
console.log(input.missing ?? 'Fallback'); // 'Fallback'
For a retry setting where zero deliberately disables retries, ?? preserves the choice. For a display label where blank text should become 'Untitled', ?? alone is not that policy: it preserves the blank text too.
Defaulting is not validation
Nullish coalescing will also preserve a negative number, a string such as 'three', and NaN. A default is not a type check or a range check. Define those separately, especially when the value affects billing, limits or destructive work.
const retries = input.retries ?? 3;
if (!Number.isInteger(retries) || retries < 0) {
throw new TypeError('retries must be a nonnegative integer');
}
This example deliberately rejects numeric strings rather than coercing them. An HTML form or an environment variable may provide text, so parse and validate at that boundary according to your application's contract. Do not silently change '0' into a different meaning by switching operators.
The fallback may not run
Both operators short-circuit. With ??, the right-hand expression is evaluated only when the left side is nullish. If the fallback calls a function, that function may not execute. Do not hide an action that must always happen inside a fallback expression.
let calls = 0;
const fallback = () => { calls += 1; return 3; };
const kept = 0 ?? fallback();
console.log(kept, calls); // 0 0
const filled = undefined ?? fallback();
console.log(filled, calls); // 3 1
Use parentheses when combining rules
JavaScript does not allow an unparenthesized mix of ?? with || or &&. That is a syntax error, not a runtime choice between defaults. Parentheses must express the intended grouping. More importantly, consider separate named steps when one field has both missing-value and blank-value rules; compact code is not automatically clearer.
What I checked locally
I ran the settings example and fallback counter in Node.js, then checked that the integer validation rejects NaN, negative numbers and numeric strings. The checks demonstrate language behavior, not the correctness of a particular application's settings model. No external services or real user settings were involved.
Before replacing every || in a project, test zero, false, an empty string, null, undefined and an invalid value for each affected field. Some existing OR expressions intentionally reject all falsy values. Fix the field's policy, not the symbol in isolation.
Comments
Post a Comment