Overview
Find out which values count as false in a condition and give missing settings a default without wiping out a real 0 or empty string.
- Falsy values: false, 0, "", null, undefined, NaN
- a || b falls back on any falsy value
- a ?? b falls back only on null and undefined
Short Course
In a condition, false, 0, "", null, undefined and NaN count as
false (falsy). The trap: || replaces every falsy value, even a real
0. ?? replaces only null and undefined.
const volume = 0;
volume || 50; // 50 (the 0 is lost!)
volume ?? 50; // 0
null ?? 50; // 50
Task
Write orDefault(value, fallback) that returns fallback only when value
is null or undefined, e.g. orDefault(0, 10) returns 0.
Test Suites
Pending
Next
Output