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

numbers · int · float · NaN · infinity

SparkyVSNaN the Trickster
Sparky saysJS has one number type (64-bit float) covering ints and decimals — plus NaN and Infinity.

There's no separate int type; all numbers are IEEE-754 doubles. That means 0.1 + 0.2 !== 0.3 (floating-point rounding) and integers are exact only up to Number.MAX_SAFE_INTEGER. NaN ("not a number") results from invalid math, and Infinity from overflow or divide-by-zero.

Power-ups you unlock

NaN the Trickster attacks — common mistakes

Boss battleShow that 0.1 + 0.2 is not exactly 0.3, then round it with toFixed.

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 a=0.1+0.2;
document.getElementById('o').textContent='0.1+0.2 = '+a+'\nrounded = '+a.toFixed(2);</script></body></html>
▶ Open the interactive comic issue
‹ Data Types · Primitives Vs ObjectsNumber Methods · ToFixed · ParseInt · IsFinite ›