JavaScript BigInt
BigInt is a primitive type for whole numbers of any size. Regular numbers lose precision above 2⁵³ − 1 (about 9 × 10¹⁵); BigInts stay exact no matter how large they get. They are useful for big IDs (for example database or social-media IDs), cryptography, and exact arithmetic on large counts.
Creating BigInts
Add an n to the end of an integer literal, or call BigInt():
const big = 1234567890123456789012345678901234567890n;
const fromNumber = BigInt(42);
const fromString = BigInt("9007199254740993");
console.log(big);
console.log(fromNumber, fromString);
console.log(typeof big);1234567890123456789012345678901234567890n
42n 9007199254740993n
bigint
console.log shows BigInts with the trailing n; String(big) or a template literal gives the plain digits.
Why we need it
const limit = Number.MAX_SAFE_INTEGER;
console.log(limit + 1);
console.log(limit + 2); // should be ...993, but isn't!
console.log(BigInt(limit) + 2n); // exact
console.log(2n ** 100n);9007199254740992
9007199254740992
9007199254740993n
1267650600228229401496703205376n
Arithmetic
BigInts support + - * ** % and /. Division truncates toward zero, because a BigInt can't hold a fraction:
console.log(10n + 5n, 10n - 5n, 10n * 5n);
console.log(10n / 3n); // 3n, not 3.333…
console.log(10n % 3n);
console.log(-7n / 2n);15n 5n 50n
3n
1n
-3n
No mixing with numbers
You can't combine a BigInt and a Number in arithmetic — JavaScript refuses rather than silently losing precision. Convert one side explicitly:
const count = 5n;
try {
console.log(count + 1);
} catch (err) {
console.log(err.name + ": " + err.message);
}
console.log(count + 1n);
console.log(count + BigInt(1));
console.log(Number(count) + 1.5); // converting to Number may lose precision for huge valuesTypeError: Cannot mix BigInt and other types, use explicit conversions
6n
6n
6.5
Comparisons, however, work across the two types:
console.log(5n === 5); // different types
console.log(5n == 5); // loose equality compares the values
console.log(2n < 3, 10n > 9.5);
console.log([4n, 1, 3n, 2].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)));false
true
true true
[ 1, 2, 3n, 4n ]
Limitations
| Not allowed | Why |
|---|---|
Math.sqrt(16n) |
Math methods only accept Numbers |
BigInt(1.5) |
must be an integer — throws a RangeError |
+10n (unary plus) |
throws a TypeError |
JSON.stringify({ id: 1n }) |
throws — JSON has no BigInt; convert to a string first |
decimals like 1.5n |
syntax error — BigInts are integers only |
Example: factorials
Factorials grow fast. With numbers, 25! is already imprecise; with BigInt it's exact:
function factorial(n) {
let result = 1n;
for (let i = 2n; i <= n; i++) {
result *= i;
}
return result;
}
console.log(factorial(25n).toString());
console.log(`30! has ${factorial(30n).toString().length} digits`);15511210043330985984000000
30! has 33 digits
Tip: BigInt arithmetic is slower than Number arithmetic. Use it when you need exact large integers, not as a general replacement for numbers.
Test yourself
How do you write the BigInt literal for one hundred?
- 100b
- 100n
- BigInt.100
- 100L
What is 7n / 2n?
- 3.5
- 3n
- 4n
- 3.5n
What happens with 1n + 1?
- 2n
- 2
- "11"
- TypeError