Floating-Point Representation Basics
What you'll learn: Why JavaScript numbers sometimes produce surprising results like 0.1 + 0.2 !== 0.3.
The IEEE 754 Standard
JavaScript doesn't store numbers the way you might expect. Every JavaScript number is stored using the IEEE 754 double-precision floating-point format (64 bits). This is the same format used by many other programming languages.
Think of it like trying to write the number "one-third" (1/3) as a decimal: you get 0.333333... The threes go on forever, so you have to round at some point. Similarly, computers store numbers in binary (base 2), and many decimal numbers that look simple to us cannot be represented exactly in binary.
Why This Matters
The number 0.1 in decimal cannot be represented exactly in binary—it's a repeating fraction in base 2, just like 1/3 is in base 10. When JavaScript stores 0.1, it stores the closest approximation it can fit in 64 bits.
This leads to precision quirks:
0.1 + 0.2 === 0.3 // false! (0.30000000000000004)
The stored values of 0.1 and 0.2 have tiny rounding errors. When added together, those errors compound slightly, producing a result that isn't exactly 0.3.
What You Can Do
For most everyday math, these tiny errors don't matter. When precision is critical (like financial calculations), you can use techniques like rounding to a fixed number of decimals with toFixed(), or use integers (work in cents instead of dollars).
Key Takeaway: JavaScript stores all numbers as 64-bit floating-point values using IEEE 754, which means some decimal numbers cannot be represented exactly and may have tiny precision errors.