Technical skill practice
JavaScript Double Question Mark: Keep Valid Falsy Values with ??
The double question mark operator, ??, selects its fallback only for null or undefined. Use it when zero, false or empty text can be meaningful supplied values. It answers a narrower question than general truthiness.
The task
Apply default settings to three deliberately falsy values and two missing-value markers. Compare with || to expose why a valid zero can be replaced unintentionally.
Runnable Node 22 example
const assert = require('node:assert/strict');
const supplied = {retries: 0, enabled: false, caption: '', note: null};
assert.equal(supplied.retries ?? 3, 0);
assert.equal(supplied.enabled ?? true, false);
assert.equal(supplied.caption ?? 'untitled', '');
assert.equal(supplied.note ?? 'none', 'none');
assert.equal(supplied.missing ?? 'fallback', 'fallback');
assert.equal(supplied.retries || 3, 3);
console.log(`retries=${supplied.retries ?? 3}; enabled=${supplied.enabled ?? true}; note=${supplied.note ?? 'none'}`);
Expected output and explanation
The fixture treats zero retries as an explicit decision and false as an explicit disabled setting. Both survive the nullish fallback. Null and the missing property trigger the alternative because their resulting values are nullish.
The || comparison produces three retries because zero is falsy. That might be correct under a different rule, but it contradicts this setting contract. Choose the operator from what counts as missing rather than which notation looks shorter.
Output from the example
retries=0; enabled=false; note=none
Pitfall to check
Parenthesize when mixing ?? with || or &&; the language does not accept every unparenthesized mixture. Also remember that a fallback supplies a value, not validation: an unexpected non-null value still passes through unchanged.
Questions, answered
Does ?? replace an empty string?
No. If empty text is invalid, check that rule separately. Nullish fallback intentionally preserves it.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.