Example 1 — if checks
Branch on truthiness.
JavaScript
if ("hi") console.log("yes");
if (0) console.log("no");How It Works
- Non-empty string is truthy.
In boolean contexts, a value is falsy if it is false, 0, -0, 0n, "", null, undefined, or NaN. Everything else is truthy—including [] and {}.
console.log(Boolean(0));
console.log(Boolean(""));
console.log(Boolean([]));
console.log(Boolean("0"));Branch on truthiness.
if ("hi") console.log("yes");
if (0) console.log("no");0 is replaced.
const n = 0 || 10;
console.log(n);Only null/undefined.
console.log(0 ?? 10);CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful