Numbers, NaN, Infinity, and Precision
JavaScript's number type is a 64-bit IEEE 754 floating-point value. This means many decimal fractions cannot be represented exactly in binary. Therefore 0.1 + 0.2 is not exactly 0.3.
The largest integer that can be represented safely as an integer by number is Number.MAX_SAFE_INTEGER. For exact arithmetic involving larger integers, use BigInt. NaN means the result is not a valid numeric value and has the unusual property that it is not equal to itself.
console.log(0.1 + 0.2);
console.log(Number.isNaN(NaN));
console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER));
console.log(9007199254740993n);Floating-point representation creates rounding behavior for some decimals. BigInt provides exact integer arithmetic for values beyond the safe integer range, although BigInt cannot be freely mixed with Number.
Do not assume decimal arithmetic is exact.
Use Number.isNaN rather than global isNaN when validating actual NaN values.
Use BigInt for integer calculations that exceed Number's safe range.
Why does 0.1 + 0.2 === 0.3 evaluate to false?