Sub topic 10
How Defaults and Rest Affect function.length
This chapter assumes you already know how default parameters and rest parameters work. It also assumes you can destructure objects. It covers two related topics: what function.length reports, and how to design a function that takes an options object.
How Defaults and Rest Affect function.length
Every function has a length property. It is the number of parameters the function expects, which is not the same as the number it declares. The count stops at the first parameter with a default value, and a rest parameter is never counted.
function f(a, b = 2, c) {}
function g(a, ...rest) {}
function h(a, b) {}
console.log(f.length); // 1 (counting stops at b = 2)
console.log(g.length); // 1 (...rest is not counted)
console.log(h.length); // 2length matters mostly to code that inspects other functions. Currying helpers and some test frameworks use it, for example, to decide how many arguments to wait for. If you add a default to a parameter in such a function, its length changes, and code that depends on it can break without any error.
Options Objects with Destructured Parameters
A long list of positional parameters is hard to read at the call site: in createUser("Ana", "admin", true), nothing says what true means. An options object passes named values instead, and destructuring in the parameter list unpacks them.
function createUser({ name, role = "viewer", active = true } = {}) {
return { name, role, active };
}
console.log(createUser({ name: "Ana", role: "admin" }));
// { name: "Ana", role: "admin", active: true }
console.log(createUser());
// { name: undefined, role: "viewer", active: true }There are two layers of defaults here:
| Default | Applies when |
|---|---|
| `role = "viewer"` | the object has no `role` property, or it is `undefined` |
| `= {}` on the whole parameter | the caller passes no object at all |
The outer = {} is easy to forget. Without it, calling the function with no arguments tries to destructure undefined and throws:
function broken({ name, role = "viewer" }) {
return { name, role };
}
try {
broken();
} catch (err) {
console.log(err instanceof TypeError); // true
}Options objects also make functions easier to change: you can add a new option without breaking existing callers, and callers can list options in any order. Per-property defaults follow the same rule as ordinary defaults, so { role: null } gives role the value null, not "viewer".
A destructured parameter counts as one parameter for length, unless it has a default:
function k({ a }) {}
function m({ a } = {}) {}
console.log(k.length); // 1
console.log(m.length); // 0Exercise
What do p.length and p({ size: null }) return for function p(id, { size = 10, color = "red" } = {}, ...tags) { return [size, color]; }?
Show answer
p.length is 1, because counting stops at the destructured parameter that has a default, and ...tags is never counted. p({ size: null }) passes the object as id and leaves the second parameter undefined, so it becomes {} and the call returns [10, "red"].
Exercise
Rewrite function connect(host, port, secure) so that callers can write connect({ host: "db" }), with port defaulting to 5432 and secure to false, and so that connect() does not throw.
Show answer
function connect({ host, port = 5432, secure = false } = {}) { return { host, port, secure }; }. The outer = {} stops connect() from throwing a TypeError.