BigInt is a JavaScript primitive for integers larger than Number.MAX_SAFE_INTEGER. It prevents precision loss for large whole numbers, but has special rules: you cannot mix BigInt with Number in arithmetic, division truncates, and JSON serialization requires explicit conversion.
What is the BigInt data type in JavaScript?
The Core Idea
BigInt is a primitive type for very large integers that cannot be represented safely by normal JavaScript number.
Use it when integer precision matters (IDs, ledgers, counters, cryptographic or financial integer math).
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
const a = Number.MAX_SAFE_INTEGER + 1;
const b = Number.MAX_SAFE_INTEGER + 2;
console.log(a === b); // true ❌ precision lost with Number
const x = 9007199254740991n;
const y = 9007199254740993n;
console.log(x === y); // false ✅ precise with BigInt
How to create | Example | Notes |
|---|---|---|
Literal with |
| Most common and readable approach. |
Constructor conversion |
| Useful for string input from API/DB. |
From safe integer number |
| Only safe when source number is already precise. |
Arithmetic Rules You Must Remember
- BigInt supports integer operators like
+,-,*,**,%. - You cannot mix
numberandbigintdirectly in arithmetic. - Division with BigInt truncates toward zero because BigInt has no fractional part.
console.log(7n / 2n); // 3n (not 3.5)
// console.log(1n + 1);
// TypeError: Cannot mix BigInt and other types
console.log(1n + BigInt(1)); // 2n
Comparisons with Number
Arithmetic mixing is disallowed, but comparison operators can compare number and bigint. Still, be careful when values exceed safe integer range.
console.log(10n > 5); // true
console.log(10n == 10); // true (loose equality)
console.log(10n === 10);// false (different types)
JSON and API Pitfall
JSON.stringify does not serialize BigInt by default and throws. Convert BigInt values to strings (or numbers when safe) before JSON transport.
const payload = { id: 9007199254740993n };
// JSON.stringify(payload);
// TypeError: Do not know how to serialize a BigInt
const safePayload = { id: payload.id.toString() };
console.log(JSON.stringify(safePayload)); // {"id":"9007199254740993"}
Use case | Choose | Reason |
|---|---|---|
Math with decimals (prices like 12.34) |
| BigInt cannot represent fractions. |
Very large integer IDs/counters |
| Avoids precision loss beyond safe integer range. |
External JSON contracts | String representation for big integers | JSON has no native BigInt type. |
Practical rules
- Stay with
numberunless you truly need > 53-bit integer precision. - Do not mix Number and BigInt in arithmetic without explicit conversion.
- Convert BigInt to string at API/JSON boundaries.
- Document type contracts clearly in shared modules.
Practical scenario
A payment platform ingests 64-bit transaction IDs from multiple services. IDs look numeric but exceed JavaScript safe integer range, causing accidental collisions when stored as Number.
Common pitfalls
- Parsing large IDs with
Number(). - Mixing
bigintandnumberin utility math. - Forgetting to convert BigInt before JSON response generation.
Add boundary tests around
Number.MAX_SAFE_INTEGER, and include contract tests verifying that large IDs remain stable through parse -> transform -> serialize cycles.Think of number as a standard measuring tape and bigint as an industrial one. Use the heavy-duty tool only when the regular tape can no longer measure accurately.
BigInt solves integer precision limits in JavaScript by representing arbitrary-size whole numbers. It is excellent for exact large integers, but requires careful interop with Number, Math APIs, and JSON serialization.
Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.