Sub topic 6
Using Earlier Parameters in Defaults
This chapter assumes you know how to write default parameter values and that defaults are evaluated each time the function is called. Here we look at what a default expression can see: which names it can refer to, and which ones cause errors.
Parameters Are Initialized Left to Right
When a function is called, JavaScript sets up its parameters one at a time, from left to right. Each parameter gets its argument, or its default if the argument is undefined. Once a parameter is initialized, every default to its right can use it.
function area(width, height = width) {
return width * height;
}
console.log(area(4)); // 16
console.log(area(4, 2)); // 8When you call area(4), width becomes 4 first. Then height has no argument, so its default width is evaluated, and by that point width already holds 4.
A default can be any expression built from earlier parameters, not only a plain copy:
function makeRange(start, end = start + 10, step = end > start ? 1 : -1) {
return { start, end, step };
}
console.log(makeRange(5)); // { start: 5, end: 15, step: 1 }
console.log(makeRange(5, 0)); // { start: 5, end: 0, step: -1 }
console.log(makeRange(0, 10, 2)); // { start: 0, end: 10, step: 2 }Common Error: Referring to a Later Parameter
A default cannot use a parameter declared after it. That parameter exists, but it has not been initialized yet. It is in the temporal dead zone, the same state a let variable is in before its declaration line runs. Reading it throws a ReferenceError.
function broken(a = b, b = 1) {
return a + b;
}
try {
broken();
} catch (err) {
console.log(err.name); // "ReferenceError"
}
console.log(broken(5)); // 6, because a's default never runsThe error happens only when the default actually runs. broken(5) works because a receives an argument and its default expression is skipped. Bugs like this can stay hidden until someone leaves out that argument.
Common Error: Referring to Body Variables
Parameters live in their own scope, which sits between the outer scope and the function body. Default expressions can see earlier parameters and outer variables. They cannot see variables declared inside the body.
const label = "outer";
function show(text = label) {
const label = "inner";
return text;
}
console.log(show()); // "outer"The default picks up the outer label, not the const label inside the body. If there were no outer label, calling show() would throw a ReferenceError.
| Default refers to | Result |
|---|---|
| An earlier parameter | Works and uses its current value |
| A later parameter | `ReferenceError` when the default runs |
| A variable in the function body | Not visible, so the outer variable is used or a `ReferenceError` is thrown |
| An outer-scope variable | Works |
Exercise
What does pad("hi") return, and what happens with pad(undefined, 3), for function pad(str = "x".repeat(width), width = 2) { return str.padStart(width, "*"); }?
Show answer
pad("hi") returns "hi". str receives an argument, so its default never runs, and "hi" is already 2 characters long. pad(undefined, 3) throws a ReferenceError, because the default for str reads width before width has been initialized. You can fix it by declaring width first: function pad(width = 2, str = "x".repeat(width)).