Technical skill practice
JavaScript Optional Parameters: Defaults Apply to Undefined
A default parameter supplies its value when the argument is omitted or is undefined. It does not replace every falsy value. Use that distinction when zero or false are valid caller choices.
The task
Build a retry description with defaults and call it with missing, undefined, null and zero values. Inspect the actual values before adding a validation policy.
Runnable Node 22 example
const assert = require('node:assert/strict');
function describe(retries = 2, enabled = true) { return {retries, enabled}; }
assert.deepEqual(describe(), {retries: 2, enabled: true});
assert.deepEqual(describe(undefined, false), {retries: 2, enabled: false});
assert.deepEqual(describe(0, false), {retries: 0, enabled: false});
assert.deepEqual(describe(null, false), {retries: null, enabled: false});
assert.deepEqual(describe('', false), {retries: '', enabled: false});
console.log(JSON.stringify([describe(), describe(0, false), describe(null, false)]));
Expected output and explanation
The omitted call receives both defaults. Explicit undefined also triggers the first default, while false survives in the second position. Zero, null and empty text each remain the supplied value because none is undefined.
The function is intentionally an inspection example, not a validated retry API. A real retry function might reject null or noninteger text; that is a separate rule. Defaults can improve call convenience without making every accepted value valid.
Output from the example
[{"retries":2,"enabled":true},{"retries":0,"enabled":false},{"retries":null,"enabled":false}]
Pitfall to check
Do not describe default parameters as “replace blanks” unless the contract actually checks blanks. Also avoid using truthy fallbacks inside the function to undo a caller’s deliberate zero after the parameter default has already been applied.
Questions, answered
Are optional and default parameters separate JavaScript features here?
A caller may omit arguments; a default defines what an undefined parameter receives. Both query wordings belong to this same omission-and-default behavior.
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.