Full answer
Writing function greet(name = 'guest') gives the parameter a value when the caller passes nothing or passes undefined. Other values count as given, including null, 0, false, and ''. If null should also trigger a default, handle it with ?? inside the function.
The default expression is evaluated at call time, every time it is needed, and only if the argument is missing. This has three consequences. A default such as [] or {} creates a new object on every call, so calls do not share state, unlike in some other languages. A default may be a function call, such as createdAt = Date.now(), and it runs only when used. A default may use parameters that come before it: function area(width, height = width). Using a later parameter, or the parameter itself, hits the temporal dead zone and throws a ReferenceError.
Default parameters live in their own scope between the function's outer scope and its body, so a closure in a default sees the parameters, not variables declared in the body.
A function's length property counts only the parameters before the first one with a default, which matters for helpers such as curry. The arguments object is not linked to defaulted parameters.
Destructured parameters can also have defaults, at both levels: function connect({ host = 'localhost', port = 80 } = {}). The outer = {} lets the function be called with no argument at all.
Required parameters can be enforced with a default that throws, for example value = required('value').
function greet(name = 'guest') {
return `Hello, ${name}`;
}
console.log(greet(), greet(undefined), greet(null), greet(''));
function addItem(item, list = []) {
list.push(item);
return list;
}
console.log(addItem('a'), addItem('b')); // a new list for each call
function area(width, height = width) {
return width * height;
}
console.log(area(4), area(4, 5));
try {
(function (a = b, b = 1) {})();
} catch (error) {
console.log('later parameter:', error.name);
}
let evaluations = 0;
const makeId = () => `id-${++evaluations}`;
function createUser(name, id = makeId()) {
return { name, id };
}
console.log(createUser('Asha'), createUser('Ravi', 'custom'), createUser('Meera'));
console.log('default evaluated', evaluations, 'times');
function connect({ host = 'localhost', port = 80 } = {}) {
return `${host}:${port}`;
}
console.log(connect(), connect({ port: 8080 }), connect({ host: 'example.com' }));
console.log(((a, b = 2, c) => {}).length, ((a, b) => {}).length);
function required(name) {
throw new TypeError(`${name} is required`);
}
function save(id = required('id')) {
return id;
}
try {
save();
} catch (error) {
console.log(error.message);
}
function nullOrMissing(value) {
value ??= 'default';
return value;
}
console.log(nullOrMissing(null), nullOrMissing(0));Run with Node.js v24.22.1 as js-69.js; two runs gave identical output. Output: Hello, guest Hello, guest Hello, null Hello, [ 'a' ] [ 'b' ] 16 20 later parameter: ReferenceError { name: 'Asha', id: 'id-1' } { name: 'Ravi', id: 'custom' } { name: 'Meera', id: 'id-2' } default evaluated 2 times localhost:80 localhost:8080 example.com:80 1 2 id is required default 0
Key points
- Defaults apply for a missing or undefined argument only.
- They are evaluated on each call, so an object default is fresh every time.
- A default can use earlier parameters but not later ones.
- function.length counts only parameters before the first default.
- Destructured parameters need an outer = {} to allow a call with no arguments.
Common mistakes
- Expecting null to trigger the default value.
- Assuming a default object is shared between calls.
- Referencing a later parameter in a default and getting a ReferenceError.
- Forgetting the outer = {} for a destructured parameter and crashing when no argument is passed.
Follow-ups
- How does function.length change when defaults are used, and why does that matter for currying?
- What is the scope of a closure created inside a default expression?
- How would you validate required arguments in a function?
Tips
- Say "evaluated on each call" and give the fresh-array example; it is the main insight.
- Mention that null does not trigger defaults, and show ?? as the fix.
- Include the destructured-parameter pattern with = {}; it is common in real code.