Gigantic Numbers

The BigInt type was added to JS to work with integers of arbitrary length. The regular Number type is limited to a safe range of ±(2^53 - 1). Anything larger turns into mush.

1. Creation

const huge = 12345678901234567890n; // 'n' at the end means BigInt
const alsoHuge = BigInt("12345678901234567890");

2. Survival Rules

  1. Don't mix: you cannot add 10n + 5. You will get an error. Convert everything to one type first: 10n + BigInt(5).
  2. Integers only: 5n / 2n results in 2n. The fractional part is simply dropped.
  3. Comparison: 5n == 5 yields true, but 5n === 5 yields false (they are different types!).
Caution

Math objects (e.g., Math.round) cannot work with BigInt.

Tip

Use BigInt for working with IDs from databases, cryptography, or financial calculations with massive sums.