Sub topic 5
Defaults Are Evaluated on Every Call
This chapter assumes you can write default parameter values and know that a default applies only when the argument is undefined. It covers when a default expression runs, and how often.
The Rule
A default value is an expression, and JavaScript re-evaluates it each time the function is called and the argument is missing. It is not computed once when the function is defined, and the result is not stored for later calls.
This matters most when the default creates an object or array:
function append(item, list = []) {
list.push(item);
return list;
}
console.log(append(1)); // [1]
console.log(append(2)); // [2], a new array is created on each callEach call without a second argument gets a fresh empty array. The second call does not see the 1 pushed during the first call.
Defaults Run Only When Needed
The default expression runs only when the parameter would otherwise be undefined. If the caller passes a value, the expression never runs and any side effects it has never happen. A counter makes this visible:
let evaluations = 0;
function nextId() {
evaluations += 1;
return evaluations;
}
function tag(label, id = nextId()) {
return `${label}#${id}`;
}
console.log(tag("a")); // "a#1"
console.log(tag("b", 99)); // "b#99", nextId() is not called
console.log(tag("c")); // "c#2"
console.log(evaluations); // 2The call that passed 99 skipped nextId(), so the count went up only twice.
Practical Consequences
Because defaults run at call time, values that change over time come out fresh on each call:
function logEvent(name, timestamp = Date.now()) {
return { name, timestamp };
}
const first = logEvent("start");
const second = logEvent("stop");
console.log(second.timestamp >= first.timestamp); // trueKeep these points in mind:
| Situation | Effect |
|---|---|
| Default is `[]` or `{}` | Each call gets a new object, so nothing leaks between calls |
| Default calls a function | That function runs only when the argument is missing |
| Default is expensive to compute | You pay the cost only on calls that omit the argument |
| Default has side effects | The side effects happen only on some calls, which can surprise readers |
Avoid side effects in default expressions unless they are the point, as in the required-parameter pattern covered later. A reader expects a signature to describe inputs, and hidden work there is easy to miss.
Common Errors
- Expecting a default object to keep state between calls. It does not. If you want shared state, use a variable outside the function.
- Expecting a default function call to run on every call. It is skipped whenever the caller passes a value other than
undefined. - Expecting
nullto trigger the default. An explicitnullis passed through unchanged, so the default expression does not run.
Exercise
Given let calls = 0; function f(x = ++calls) { return x; }, what do f(), f(10), f(undefined), and f(null) return in that order, and what is calls afterward?
Show answer
f() returns 1, f(10) returns 10, f(undefined) returns 2, and f(null) returns null. The default runs only for the first and third calls, so calls is 2.
Exercise
Why does function add(item, bag = {}) { bag[item] = true; return bag; } return { x: true } and then { y: true } for the calls add("x") and add("y"), rather than { x: true, y: true }?
Show answer
The default {} is evaluated again on every call that omits bag, so each call creates and mutates a new, separate object.