Sub topic 8
Default Values with || and the 0 and Empty-String Trap
This chapter assumes you already know the falsy values, that || returns one of its operands, and that || skips its right side once the left side is truthy. Here you will see how those rules combine into a common pattern for setting defaults, and where that pattern quietly breaks.
The Classic Default-Value Idiom
For many years, JavaScript developers supplied defaults by writing value || fallback. If value is truthy, || returns it. Otherwise || returns fallback.
function greet(name) {
const displayName = name || "Guest";
return `Hello, ${displayName}!`;
}
console.log(greet("Ada")); // "Hello, Ada!"
console.log(greet()); // "Hello, Guest!"
console.log(greet(undefined)); // "Hello, Guest!"
console.log(greet(null)); // "Hello, Guest!"This works well when every falsy value really means "nothing was given." The pattern is short and readable, so you will see it in a lot of older code.
The Trap: Valid Values That Happen to Be Falsy
The || operator does not ask whether a value is missing. It only asks whether the value is truthy. That means 0, "", false and NaN all get replaced, even when the caller passed them on purpose.
function createPlayer(options) {
const volume = options.volume || 50;
const name = options.name || "Player";
const muted = options.muted || true;
return { volume, name, muted };
}
console.log(createPlayer({ volume: 0, name: "", muted: false }));
// { volume: 50, name: 'Player', muted: true }The caller asked for silence, a blank name and an unmuted player. The function ignored all three. The muted line can never produce false, so that option is broken outright.
| Input value | `value \ | \ | 50` | Is the replacement what you wanted? |
|---|---|---|---|---|
| `undefined` | `50` | Yes | ||
| `null` | `50` | Usually | ||
| `0` | `50` | Often no | ||
| `""` | `50` | Often no | ||
| `false` | `50` | Often no | ||
| `NaN` | `50` | Depends | ||
| `7` | `7` | Yes |
When || Is Still the Right Choice
Use || when every falsy value should be treated as "use the default." A display label is a good example. An empty username probably should show "Anonymous" instead of a blank space.
const username = "";
console.log(username || "Anonymous"); // "Anonymous"Ask yourself one question: could 0, "" or false be a legitimate value here? If the answer is yes, || is the wrong tool.
Safer Alternatives
You can check for a missing value explicitly:
function createPlayer(options) {
const volume = options.volume !== undefined ? options.volume : 50;
const name = options.name !== undefined ? options.name : "Player";
const muted = options.muted !== undefined ? options.muted : true;
return { volume, name, muted };
}
console.log(createPlayer({ volume: 0, name: "", muted: false }));
// { volume: 0, name: '', muted: false }For function arguments, default parameters do the same check for you. They apply only when the argument is undefined.
function setVolume(level = 50) {
return level;
}
console.log(setVolume()); // 50
console.log(setVolume(undefined)); // 50
console.log(setVolume(0)); // 0
console.log(setVolume(null)); // nullExercise
A function contains const retries = config.retries || 3;. What value does retries get when config.retries is 0, and why could that be a bug?
Show answer
retries becomes 3, because 0 is falsy and || returns its right operand. If the caller set 0 on purpose to disable retries, the function overrides that choice and retries three times anyway.
Exercise
Rewrite const showHints = settings.showHints || true; so that a caller can turn hints off by passing false.
Show answer
const showHints = settings.showHints !== undefined ? settings.showHints : true; The original line always gives true, because false || true is true.