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
- Don't mix: you cannot add
10n + 5. You will get an error. Convert everything to one type first:10n + BigInt(5). - Integers only:
5n / 2nresults in2n. The fractional part is simply dropped. - Comparison:
5n == 5yields true, but5n === 5yields 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.