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

NaN · why isNaN is weird

LambdaVSThe Mutator
Lambda saysNaN is the only value not equal to itself — use Number.isNaN to detect it.

NaN === NaN is false, so you can't test for it with equality. Use Number.isNaN(x) (the global isNaN coerces and gives false positives). NaN propagates: any math with it yields NaN, so it often signals an earlier bad conversion you should trace back.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleShow NaN !== NaN, then detect it correctly with Number.isNaN.

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 bad=Number('xyz');
document.getElementById('o').textContent='NaN===NaN: '+(NaN===NaN)+'\nisNaN(bad): '+Number.isNaN(bad);</script></body></html>
▶ Open the interactive comic issue
‹ Truthy / Falsy GotchasStrict Mode · "Use Strict" ›