Sub topic 4
Why Defaults Apply to undefined but Not null
This chapter assumes you already know how to declare a function, how to write a default parameter such as name = "guest", and that a missing argument arrives as undefined. It covers exactly when a default is used and when it is skipped.
The Rule: Only undefined Triggers a Default
A default value is used only when the parameter's value is strictly undefined. That happens in two ways. The caller can leave the argument out, or the caller can pass undefined explicitly. Every other value is passed through unchanged, including null, 0, "", false, and NaN.
function greet(name = "guest", greeting = "Hello") {
return `${greeting}, ${name}!`;
}
console.log(greet()); // "Hello, guest!"
console.log(greet(undefined, "Hi")); // "Hi, guest!"
console.log(greet(null)); // "Hello, null!"
console.log(greet("")); // "Hello, !"
console.log(greet(0)); // "Hello, 0!"Passing undefined explicitly is the standard way to skip a parameter in the middle of the list while still giving a later one.
Why the Language Draws the Line Here
JavaScript gives the two "empty" values different meanings. undefined means nothing was supplied. null means the caller chose to say "no value" on purpose. If defaults replaced null, a caller could never pass "intentionally empty" to a function that has a default. Because the check is a strict === undefined, other falsy values such as 0 and false also stay valid arguments.
The Common Error: null from Data Sources
JSON has no undefined, so APIs and databases often send null for missing fields. Code that passes that null along gets no default:
function formatPrice(currency = "USD") {
return `Price in ${currency}`;
}
const order = JSON.parse('{"currency": null}');
console.log(formatPrice(order.currency)); // "Price in null"If your function should treat null as missing too, normalize it in the body with the nullish coalescing operator ??. That operator replaces only null and undefined:
function formatPrice(currency) {
currency = currency ?? "USD";
return `Price in ${currency}`;
}
console.log(formatPrice(null)); // "Price in USD"
console.log(formatPrice(undefined)); // "Price in USD"
console.log(formatPrice("EUR")); // "Price in EUR"Choosing an Approach
| Goal | Technique | Replaces | Column | Column |
|---|---|---|---|---|
| Default only when omitted | `param = value` in the signature | `undefined` | ||
| Treat null as missing too | `param = param ?? value` in the body | `undefined`, `null` | ||
| Replace any falsy value | `param = param \ | \ | value` | `undefined`, `null`, `0`, `""`, `false`, `NaN` |
Exercise
Given function setVolume(level = 50) { return level; }, what do setVolume(), setVolume(null), setVolume(0), and setVolume(undefined) return, and how would you change the function so that null also gives 50 while 0 stays 0?
Show answer
They return 50, null, 0, and 50. The default runs only for undefined. To also cover null, write function setVolume(level) { level = level ?? 50; return level; }. The ?? operator replaces only null and undefined, so 0 is kept.
Exercise
A caller wants to pass a custom greeting but keep the default name in greet(name = "guest", greeting = "Hello"). What call should they write?
Show answer
greet(undefined, "Hi"). An explicit undefined triggers the default for name, so the result is "Hi, guest!".