Sub topic 17
BigInt: Integers Beyond the Safe Range
This chapter assumes you have read the chapter on the safe integer range. You should know that Number.MAX_SAFE_INTEGER is 2^53 − 1 and that Numbers above it lose precision. You should also be comfortable with typeof and strict equality.
The Working Model
BigInt is a separate primitive type for whole numbers of any size. It stores the exact value with as many bits as needed, so it never rounds. You create one by adding n to an integer literal or by calling BigInt().
console.log(2 ** 64); // 18446744073709552000 (rounded)
console.log(2n ** 64n); // 18446744073709551616n (exact)
const a = 9007199254740993n;
console.log(a + 1n); // 9007199254740994n
console.log(typeof a); // "bigint"
console.log(BigInt("123456789012345678901234567890")); // exact
console.log(BigInt(42)); // 42nPass large values to BigInt() as strings. If you write BigInt(9007199254740993), the Number literal has already been rounded before BigInt receives it.
Arithmetic Rules
BigInt supports +, -, *, **, /, and %. Division truncates toward zero, and % keeps the sign of the dividend, the same way it does for Numbers.
console.log(10n / 3n); // 3n
console.log(-7n / 2n); // -3n
console.log(-7n % 3n); // -1nCommon Errors
| Mistake | Result |
|---|---|
| `1n + 1` | TypeError: cannot mix BigInt and other types |
| `+5n` | TypeError: unary plus is not allowed |
| `Math.max(1n, 2n)` | TypeError: Math functions accept only Numbers |
| `BigInt(1.5)` | RangeError: the value is not an integer |
| `2n ** -1n` | RangeError: negative exponent |
| `JSON.stringify({ id: 1n })` | TypeError: BigInt has no JSON form |
You have to convert explicitly to mix the types, and you choose the direction:
const big = 2n ** 60n;
const small = 5;
console.log(big + BigInt(small)); // 1152921504606846981n
console.log(Number(big) + small); // 1152921504606847000 (precision lost)
// Comparisons across types are allowed
console.log(1n < 2); // true
console.log(1n == 1); // true
console.log(1n === 1); // false (different types)
// Serialize by converting to a string first
console.log(JSON.stringify({ id: (2n ** 64n).toString() }));
// {"id":"18446744073709551616"}Choosing Between Number and BigInt
BigInt fits database IDs, 64-bit counters, cryptographic math, and exact factorials. BigInt arithmetic is slower than Number arithmetic. It also has no fractions and cannot be used with Math, so don't use it as a default numeric type.
Exercise
An API returns the ID "9007199254740993" as a string. Why does BigInt(Number(id)) give the wrong value, and what should you write instead?
Show answer
Number(id) rounds the value to 9007199254740992 because it exceeds Number.MAXSAFEINTEGER, and BigInt then stores that rounded value exactly. Write BigInt(id) so the string is parsed directly into an exact 9007199254740993n.
Exercise
What do 7n / 2n and -7n / 2n evaluate to, and what happens with 7n / 2?
Show answer
7n / 2n is 3n and -7n / 2n is -3n, because BigInt division truncates toward zero. 7n / 2 throws a TypeError because BigInt and Number cannot be mixed in arithmetic.