freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #49 of 61

truthy / falsy gotchas

LambdaVSThe Mutator
Lambda saysTruthy/falsy coercion causes subtle bugs — know the falsy list cold.

Conditions coerce, and the surprises are real: [] and {} are truthy, so if (arr) is true even for an empty array (check arr.length). "0" is truthy but 0 is falsy. Memorize the six falsy values and reach for explicit checks when intent matters.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleShow that an empty array is truthy, then correctly test it for emptiness.

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 arr=[]; const truthy=!!arr; const empty=arr.length===0;
document.getElementById('o').textContent='[] truthy='+truthy+' empty='+empty;</script></body></html>
▶ Open the interactive comic issue
‹ Typeof Vs InstanceofNaN · Why IsNaN Is Weird ›