BigInt: Beyond Number.MAX_SAFE_INTEGER
You'll learn how to represent integers larger than JavaScript's safe number limit using BigInt.
The Number Precision Problem
JavaScript's Number type can safely represent integers only up to Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). Beyond this, precision breaks down—calculations become unreliable because Number uses floating-point representation. If you're working with massive IDs, timestamps in nanoseconds, or cryptographic operations, you'll hit this wall quickly.
Think of regular numbers like a ruler that only goes up to 100 inches. Once you need to measure something longer, the ruler simply can't help you anymore.
Enter BigInt
BigInt is a primitive type that represents integers of arbitrary size—no upper limit. Create one by appending n to an integer literal or using the BigInt() constructor:
const safe = 9007199254740991;
const unsafe = 9007199254740992; // Precision starts breaking
const bigSafe = 9007199254740991n;
const bigUnsafe = 9007199254740992n; // Still precise!
// Or use the constructor
const fromString = BigInt("123456789012345678901234567890");
Working with BigInt
You can use standard arithmetic operators, but you cannot mix BigInt with regular numbers:
const big = 100n;
const result = big + 50n; // 150n ✓
// const mixed = big + 50; // ❌ TypeError
// Convert when needed
const converted = big + BigInt(50); // 150n ✓
BigInt values cannot have decimal parts—they're strictly integers. Division truncates toward zero.
Key Takeaway: Use BigInt (with the n suffix) when you need integer precision beyond Number.MAX_SAFE_INTEGER, but remember you cannot mix BigInt and Number types in operations without explicit conversion.