What is the BigInt data type in JavaScript?

HighIntermediateJavascript
Quick Answer

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.

Answer

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).

JAVASCRIPT
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 n suffix

12345678901234567890n

Most common and readable approach.

Constructor conversion

BigInt('12345678901234567890')

Useful for string input from API/DB.

From safe integer number

BigInt(42)

Only safe when source number is already precise.

BigInt values represent whole numbers only (no decimal fraction).

Arithmetic Rules You Must Remember

  • BigInt supports integer operators like +, -, *, **, %.
  • You cannot mix number and bigint directly in arithmetic.
  • Division with BigInt truncates toward zero because BigInt has no fractional part.
JAVASCRIPT
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.

JAVASCRIPT
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.

JAVASCRIPT
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)

number + decimal strategy

BigInt cannot represent fractions.

Very large integer IDs/counters

bigint

Avoids precision loss beyond safe integer range.

External JSON contracts

String representation for big integers

JSON has no native BigInt type.

Pick BigInt when integer correctness matters more than universal interoperability.

Practical rules

    • Stay with number unless 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 bigint and number in utility math.
      • Forgetting to convert BigInt before JSON response generation.
Trade-off or test tip
Add boundary tests around Number.MAX_SAFE_INTEGER, and include contract tests verifying that large IDs remain stable through parse -> transform -> serialize cycles.

Still so complicated?

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.

Summary

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.

Similar questions
Guides
Preparing for interviews?

Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.