Numbers, BigInt & Math
Explore how JavaScript handles numbers, why 0.1 + 0.2 is broken, and when to use BigInt for massive integers.
The Number Type
Unlike many languages that have separate types for integers (int) and decimals (float), JavaScript historically only had one: Number.
Every Number in JavaScript is a 64-bit floating-point value (IEEE 754). This means there is no true 'integer' type under the hood; even 42 is technically stored as 42.0.
The floating point trap
Because computers operate in binary (base-2), they struggle to cleanly represent certain base-10 decimals, much like how 1/3 cannot be written cleanly in decimal (0.3333...).
This leads to the famous 0.1 + 0.2 === 0.30000000000000004 bug. When dealing with currency or precise measurements, you must account for this floating-point math rounding error.
Math and BigInt
JavaScript provides a built-in Math object loaded with helpful static methods like Math.round(), Math.floor(), Math.max(), and constants like Math.PI.
Because the 64-bit Number uses some bits for decimals, its maximum safe integer is 9007199254740991 (Number.MAX_SAFE_INTEGER).
To represent numbers larger than this, JavaScript introduced BigInt. You create one by appending an n to the end of a literal, like 999999999999999999n. BigInt can be infinitely large, but it cannot contain decimals.
The Floating Point Flaw
Check out what happens when you try to do basic decimal math in JavaScript, and see how the BigInt type avoids the maximum safe integer limit.
Math Evaluator
Select an expression
Due to 64-bit floating point binary representation, 0.1 and 0.2 cannot be stored perfectly. The tiny remainder spills over in base-10 math.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- JavaScript standard Numbers are 64-bit floating point values (IEEE 754).
- Decimal math in JavaScript can be imprecise (e.g.,
0.1 + 0.2). BigIntcan store integers larger thanNumber.MAX_SAFE_INTEGERbut cannot hold decimals.- You cannot directly mix
NumberandBigIntin mathematical operations.
Done with this concept?
Mark it complete to track your progress. No login needed.