JavaScript Number can represent integers exactly only up to Number.MAX_SAFE_INTEGER (2^53 - 1). When you need larger integer values without losing precision, use BigInt.
Create a BigInt
const a = 123456789012345678901234567890n;
const b = BigInt('123456789012345678901234567890');The n suffix creates a BigInt literal. BigInt() is useful when converting a string or an integer-valued Number.
Arithmetic
const a = 12345678901234567890n;
const b = 98765432109876543210n;
console.log(a + b); // 111111111011111111100n
console.log(b / a); // 8n
BigInt division truncates the fractional part toward zero:
console.log(7n / 2n); // 3n
Comparisons
console.log(1000n < 2000n); // true
console.log(1000n === 1000n); // true
console.log(1000n === 1000); // false
console.log(1000n == 1000); // true, due to coercion
Prefer strict equality unless coercion is intentional.
Convert Between Number and BigInt
const value = BigInt(42); // 42n
Converting a large BigInt to Number can lose precision:
const large = 12345678901234567890n;
const number = Number(large);Also, arithmetic cannot mix Number and BigInt directly:
// 1n + 1 // TypeError
1n + BigInt(1); // 2n
JSON Serialization
JSON.stringify does not serialize BigInt values directly. Convert them to strings or provide a replacer:
const payload = { value: 9007199254740993n };
const json = JSON.stringify(payload, (_, value) =>
typeof value === 'bigint' ? value.toString() : value
);The receiving side must know that the string represents an integer and convert it back when appropriate.
When BigInt Is Useful
BigInt is useful for database IDs larger than JavaScript’s safe integer range, counters, timestamps or protocol fields that use 64-bit integers, and algorithms that genuinely require arbitrary-size integers.
It is not automatically the right choice for financial decimal arithmetic: BigInt stores integers, not decimal fractions. Financial applications often represent minor units as integers or use a decimal arithmetic library.
Conclusion
Use BigInt when exact integer values can exceed JavaScript’s safe Number range. Keep it separate from floating-point arithmetic, convert explicitly at boundaries, and define a serialization strategy for APIs and JSON.