Numbers, BigInt & Math

Explore how JavaScript handles numbers, why 0.1 + 0.2 is broken, and when to use BigInt for massive integers.

JavaScript6 min readConcept 6 of 62

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

const result = 0.1 + 0.2;
console.log(result);
Console Output
0.30000000000000004

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.

iWhen accuracy matters (like with money), either convert decimals to whole integers before calculating, or use a specialized library to handle high-precision math.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What is the result of `0.1 + 0.2 === 0.3`?
  2. 2How do you define a BigInt literal?
  3. 3Can a BigInt hold a decimal value (e.g., 5.5n)?

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).
  • BigInt can store integers larger than Number.MAX_SAFE_INTEGER but cannot hold decimals.
  • You cannot directly mix Number and BigInt in mathematical operations.

Done with this concept?

Mark it complete to track your progress. No login needed.