Truthiness describes how JavaScript coerces values to booleans in boolean contexts. The falsy values are false, null, undefined, 0, "", and NaN; other values are treated as true. section 3.3.2
Use truthiness carefully when supplying fallback values with the logical OR operator:
- firstCity || "London" uses London when firstCity is undefined or null. section 3.3.2
- The same expression also replaces valid falsy values such as 0, "", and NaN, which can produce unexpected results. section 3.3.2
- Use the nullish coalescing operator when only null and undefined should trigger the fallback. section 3.3.2
Preserving a zero tax rate
With taxRate || 10, both an undefined rate and a zero rate produce 10%. Replacing it with taxRate ?? 10 produces 10% for undefined and 0% for zero. section 3.3.2
...
let hatPrice = 100;
console.log(`Hat price: ${hatPrice}`);
let bootsPrice = "100";
console.log(`Boots price: ${bootsPrice}`);
if (hatPrice == bootsPrice) {
...
Browse the surrounding coercion examples before comparing fallback behavior and strict equality. section 3.3.2





