Sub topic 3
Giving Parameters Default Values
This chapter assumes you can declare and call functions, use let and const, and read template literals. You should also know from the previous chapter that a parameter with no matching argument receives undefined.
The Problem Defaults Solve
Before ES2015, developers filled in missing arguments by hand inside the function body:
function greetOld(name, greeting) {
if (greeting === undefined) greeting = "Hello";
if (name === undefined) name = "guest";
return `${greeting}, ${name}!`;
}
console.log(greetOld()); // "Hello, guest!"This works, but the fallback values are hidden inside the body. A reader has to scan the code to learn what the function expects.
The Default Parameter Syntax
A default parameter is a parameter written with = value in the parameter list. If the caller leaves that argument out, the parameter takes the default:
function greet(name = "guest", greeting = "Hello") {
return `${greeting}, ${name}!`;
}
console.log(greet()); // "Hello, guest!"
console.log(greet("Ana")); // "Hello, Ana!"
console.log(greet(undefined, "Hi")); // "Hi, guest!"
console.log(greet(null)); // "Hello, null!"The signature now documents itself: name falls back to "guest" and greeting falls back to "Hello".
Look at the last two calls. Passing undefined explicitly triggers the default, so you can skip name and still supply greeting. Passing null does not trigger the default. The next chapter explains this rule in detail.
What a Default Can Be
A default can be any expression, not only a literal. It can be an array, an object, a function call, or arithmetic:
function makeTag(label = "item", id = Date.now(), tags = []) {
return { label, id, tags };
}
console.log(makeTag("box"));Later chapters cover two details here: defaults run fresh on every call, and a default can use parameters declared before it.
Common Mistakes
| Mistake | What happens | Fix | Column | Column |
|---|---|---|---|---|
| Expecting `null` to trigger the default | `null` is used as the value | Pass `undefined` or omit the argument | ||
| Using `name = name \ | \ | "guest"` in the body | `""`, `0`, and `false` get replaced too | Use a default parameter |
| Placing defaulted parameters first | Callers must pass `undefined` to skip them | Put optional parameters last |
The || mistake catches people often. count || 10 turns a deliberate 0 into 10. A default parameter only fills in missing values, so 0 stays 0:
function repeat(text, times = 3) {
return text.repeat(times);
}
console.log(repeat("ab")); // "ababab"
console.log(repeat("ab", 0)); // "" (0 is kept)Exercises
Exercise
Given function pad(str, width = 5, char = " ") { return str.padStart(width, char); }, what does pad("7", undefined, "0") return?
Show answer
"00007". Passing undefined for width triggers its default of 5, and char is "0".
Exercise
Rewrite function retry(attempts) { attempts = attempts || 3; return attempts; } so that retry(0) returns 0 and retry() returns 3.
Show answer
function retry(attempts = 3) { return attempts; }. The default applies only when attempts is undefined, so 0 is kept.