JavaScript › Numbers, Math & BigInt

Why does 0.1 + 0.2 not equal 0.3 exactly in JavaScript?

Beginner

Answer

JavaScript numbers use the IEEE 754 double-precision floating-point format, which can't represent most decimal fractions exactly in binary — this is a property of floating-point math shared by nearly all programming languages, not a JavaScript-specific bug.
console.log(0.1 + 0.2);            // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3);    // false
Real-world example Avoiding == comparisons on computed floating-point totals in a shopping cart or financial calculation.

Common follow-ups: How do you correctly compare two floating-point numbers for 'close enough' equality?