freecoding.school100% FREE · NO SIGNUP
Script SpringsISSUE #14 of 29

booleans · truthy · falsy

SparkyVSNaN the Trickster
Sparky saysBooleans are true/false; many values coerce to them as "truthy" or "falsy".

Conditions need a boolean, so JS coerces. The falsy values are exactly: false, 0, "", null, undefined, NaN (and 0n). Everything else is truthy — including "0", [], and {}. Boolean(x) or !!x converts explicitly.

Power-ups you unlock

NaN the Trickster attacks — common mistakes

Boss battleLog the truthiness of 0, "", "0", [], and {} with !! — note which surprise you.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const v=[0,'','0',[],{}].map(x=>JSON.stringify(x)+' → '+!!x);
document.getElementById('o').textContent=v.join('\n');</script></body></html>
▶ Open the interactive comic issue
‹ String Methods · Slice · Split · ReplaceOperators · Arithmetic ›