Whole Numbers Without Limits
JavaScript BigInt
When `number` isn't big enough — `BigInt` lets you work with integers of arbitrary size. Reach for it when precision matters.
What you'll learn
- Write `BigInt` literals with the `n` suffix
- Know when ordinary numbers stop being accurate
- Avoid mixing `BigInt` and `Number` in math
Regular number values stop being accurate above
Number.MAX_SAFE_INTEGER (about 9 quadrillion). For most apps,
that’s plenty. For database IDs, cryptography, or scientific
computing, it’s not enough — and that’s where BigInt comes in.
Creating a BigInt
Add an n suffix to an integer literal, or call BigInt(value).
const big = 9007199254740993n; // ← n makes it BigInt
const veryBig = 12345678901234567890n;
console.log(big); // 9007199254740993n
console.log(typeof big); // "bigint"
const fromString = BigInt("12345"); // also works
console.log(fromString); // 12345n When You Need It
Compare ordinary number vs BigInt for the same large integer:
console.log(9007199254740993); // 9007199254740992 ❌ rounded
console.log(9007199254740993n); // 9007199254740993n ✅ exact If you’re working with:
- Database IDs beyond 2^53
- Timestamps in nanoseconds
- Cryptographic numbers
- Counts where every unit matters (financial reconciliation)
…use BigInt.
Math Works the Same Way
The usual operators work on BigInt — but only with other BigInts.
const a = 1000000000000n;
const b = 2000000000000n;
console.log(a + b); // 3000000000000n
console.log(a * b); // 2000000000000000000000000n
console.log(b / a); // 2n ← integer division (no decimals)
console.log(b % a); // 0n const big = 100n;
const small = 5;
// console.log(big + small); // TypeError: Cannot mix BigInt and other types
console.log(big + BigInt(small)); // 105n
console.log(Number(big) + small); // 105 (back to regular number) Comparison Works Loosely
BigInt and Number can be compared with ===, <, >, etc. —
just not added or multiplied together.
console.log(10n === 10); // false ← different types
console.log(10n == 10); // true ← loose equality converts
console.log(10n < 20); // true ← comparison works When NOT to Use BigInt
- Decimals —
BigIntis integer-only.100n / 3ngives33n, not33.33. - Performance-critical math —
BigIntoperations are slower than regularnumber. - When
Number.MAX_SAFE_INTEGERis enough — most of the time, it is.
Up Next
The last primitive type for a while — booleans.
JavaScript Booleans →