Default Parameters
A default parameter fills in a value when the caller omits that argument or passes undefined. The default is an expression, evaluated at call time, from left to right.
Detailed Explanation
Before defaults, people wrote arg = arg || fallback at the top of every function. That pattern has a bug: it also replaces 0, "", and false. Default parameters exist to replace only a missing argument, and they treat undefined as missing.
function greet(name = "friend") {
return `hello ${name}`;
}
greet() and greet(undefined) both use "friend". greet(null) uses null, because null is a value. greet("") uses the empty string. That is the rule: only undefined triggers the default.
The default is an expression, not a frozen snapshot. It runs when that parameter needs a value, on that call. function page(limit = readConfig()) calls readConfig only when limit is omitted. That lets defaults be cheap when unused, and it lets them see the current time, the current config, or earlier parameters.
Defaults can refer to parameters to their left. function slice(arr, start = 0, end = arr.length) is idiomatic. They cannot refer to parameters to their right: those bindings are still in the temporal dead zone. function bad(a = b, b = 1) throws when a needs its default.
A default creates a temporal dead zone for that parameter, like let. The parameter list is its own scope. A default expression can close over outer names, and a parameter name shadows outer names of the same spelling.
Default values work on destructured parameters too: function draw({ x = 0, y = 0 } = {}). The = {} on the whole parameter is what lets you call draw() with no argument. Without it, draw() tries to destructure undefined and throws.
What it costs you: assuming null is "empty". APIs that pass null to mean "use the default" will not hit the default. Document whether callers should omit the argument, pass undefined, or pass null, and pick one. Also, a default that allocates (= [] or = {}) allocates a new object per omitted call, which is what you want. The old fn.cache = fn.cache || [] pattern shared one array across calls; a parameter default does not.
Reach for defaults when a function has a sensible fallback and you want the signature to say so. Keep default expressions cheap and free of surprising side effects.
Code Examples
function price(amount, tax = 0.1) {
return amount * (1 + tax);
}
console.log(price(100)); // => 110
console.log(price(100, undefined)); // => 110
console.log(price(100, 0)); // => 100 (0 is kept)
console.log(price(100, null)); // => 100 (null coerces in the math: 1 + null === 1)function take(list, start = 0, end = list.length) {
return list.slice(start, end);
}
console.log(take(["a", "b", "c"])); // => [ 'a', 'b', 'c' ]
console.log(take(["a", "b", "c"], 1)); // => [ 'b', 'c' ]function draw({ x = 0, y = 0 } = {}) {
return { x, y };
}
console.log(draw()); // => { x: 0, y: 0 }
console.log(draw({ x: 5 })); // => { x: 5, y: 0 }
function drawRequired({ x = 0, y = 0 }) {
return { x, y };
}
try {
drawRequired();
} catch (err) {
console.log(err.name); // => TypeError (cannot destructure undefined)
}Common Gotchas
Only `undefined` triggers the default
`null`, `0`, and `""` are used as-is. If your API uses `null` for "missing", normalise it before the call or inside the body.
Defaults cannot read parameters to their right
those names are in the TDZ. Order parameters so later ones depend on earlier ones.
Destructure defaults need a whole-parameter fallback
`{ x = 0 } = {}` handles a missing argument. `{ x = 0 }` alone does not.
Defaults run at call time
`function f(x = Date.now())` gets a new timestamp each omitted call, not one timestamp from definition time.