BigInt is a JavaScript primitive type for whole numbers of any size. A normal Number starts to round whole numbers above about 9 quadrillion; a BigInt never does. It only stops growing when your computer runs out of memory.
You create a BigInt by writing n after the digits, as in 123n, or by calling BigInt(123). Its typeof is "bigint", a separate type from "number". BigInt was added in ES2020 and works in all modern browsers and in Node.js 10.4 and later.
BigInt is not a constructor.
Call BigInt(value) without new. Writing new BigInt(5) throws a TypeError.
Syntax
Creating a BigInt
There are two ways: a literal with n, or the BigInt() function. The comments show each result; a trailing n marks a BigInt.
JavaScript
const a = 123n; // a BigInt literal
const b = BigInt(123); // from a whole Number
const c = BigInt("9007199254740993"); // from a string of digits
typeof a; // "bigint"
a === b; // true
c; // 9007199254740993n
0xffn; // 255n (hex literals work too)
BigInt(1.5); // RangeError (not a whole number)
BigInt("1.5"); // SyntaxError (not an integer string)
new BigInt(5); // TypeError (BigInt is not a constructor)
Why
Why BigInt Exists: The Safe Integer Limit
A JavaScript Number is a 64-bit floating-point value. It can store whole numbers exactly only up to 253 - 1, which is 9007199254740991 and is available as Number.MAX_SAFE_INTEGER. A safe integer is a whole number that a Number can store exactly, with no rounding. Above that limit, neighbouring whole numbers share the same Number value, so digits silently change:
Number.isSafeInteger() tells you whether a Number is still exact. When it says false, or when the values come from a 64-bit system, switch to BigInt.
Rules
Operators and the No-Mixing Rule
BigInts work with +, -, *, **, %, /, and the bitwise operators. Division drops the fraction and rounds toward zero, because a BigInt can only be whole.
JavaScript
10n + 5n; // 15n
10n - 15n; // -5n
6n * 7n; // 42n
2n ** 64n; // 18446744073709551616n
7n / 2n; // 3n (the .5 is dropped)
-7n / 2n; // -3n (rounds toward zero)
7n % 2n; // 1n
-(5n); // -5n (unary minus works)
1n << 10n; // 1024n
5n & 3n; // 1n
+5n; // TypeError (unary plus is not allowed)
5n >>> 1n; // TypeError (no unsigned right shift)
2n ** -1n; // RangeError (negative exponent)
1n / 0n; // RangeError (division by zero)
You cannot mix BigInt and Number in arithmetic.1n + 1 throws a TypeError (Node.js and Chrome say Cannot mix BigInt and other types, use explicit conversions). Convert one side yourself:
JavaScript
1n + 1; // TypeError
1n + BigInt(1); // 2n
Number(1n) + 1; // 2
"total: " + 5n; // "total: 5" (joining with a string is allowed)
Compare
Comparing BigInt and Number
Comparisons are the exception to the no-mixing rule: <, >, and == compare the mathematical values. Strict === also checks the type, so a BigInt is never === a Number.
To sort BigInts by value, pass a comparator that returns a Number, such as (a, b) => (a < b ? -1 : a > b ? 1 : 0). The usual a - b returns a BigInt, and Array sort() throws a TypeError for it.
Convert
Conversions and Limits
Converting between the two types is always explicit. Watch the edges: a huge BigInt loses digits when it becomes a Number, Math functions accept only Numbers, and JSON.stringify() has no BigInt format.
The replacer function turns every BigInt into a string while the rest of the object is serialized as usual. On the other side, BigInt("1") turns the string back into 1n.
Uses
When to Use BigInt
Huge exact results, such as factorials and combinations: 25! has 26 digits, far past the safe limit.
Money in whole cents, where every cent must add up exactly, even for very large totals.
64-bit IDs from databases (BIGINT columns) or services such as Twitter/X, whose “snowflake” IDs are too big for a Number.
Hash and crypto math that needs exact 32-bit or 64-bit wrap-around, with BigInt.asUintN().
Nanosecond timestamps, for example from process.hrtime.bigint() in Node.js.
For everyday counting, prices with decimals, or anything that needs Math, a normal Number is simpler and usually faster. Use BigInt when the numbers really are whole and really are big.
Code
JavaScript Examples
Three short programs that run in Node.js and in any modern browser tab.
Example 1 — Where Number Stops Being Exact
Write the same 16-digit value as a Number and as a BigInt. The Number quietly changes the last digit; the BigInt keeps it.
The safe limit is 2^53 - 1.Number.MAX_SAFE_INTEGER is the largest whole number a Number can store exactly: 9007199254740991.
Two past the limit, the Number is wrong. The literal 9007199254740993 cannot be stored as a Number, so JavaScript rounds it to the nearest value it can store, 9007199254740992.
The BigInt keeps every digit. With an n suffix, or by converting the safe maximum and adding 2n, the result is exactly 9007199254740993. String() prints it without the n.
Example 2 — The Mixing Rule and Comparisons
Arithmetic between a BigInt and a Number throws, so convert one side. Comparisons, however, work across both types.
Mixing types throws.big + 1 adds a BigInt and a Number, so JavaScript throws a TypeError instead of guessing which type you meant.
Convert one side explicitly.BigInt(1) keeps the result a BigInt (11n), while Number(big) makes it a Number (11). Integer division 7n / 2n drops the remainder and gives 3n.
Comparisons cross the types.== and > compare values, so 1n == 1 and 2n > 1 are true. === also compares the type, so 1n === 1 is false.
Example 3 — Exact Money With Whole Cents
Floating-point Numbers cannot store 0.1 exactly. Counting money in whole cents with BigInt keeps every cent, and / plus % split a bill fairly.
JavaScript
const priceCents = 1999n; // $19.99 as whole cents
const totalCents = priceCents * 3n; // 5997n
const dollars = totalCents / 100n;
const cents = totalCents % 100n;
console.log("total: $" + dollars + "." + String(cents).padStart(2, "0")); // total: $59.97
const people = 4n;
console.log("each pays " + totalCents / people + " cents"); // each pays 1499 cents
console.log("left over: " + totalCents % people + " cent"); // left over: 1 cent
console.log(0.1 + 0.2); // 0.30000000000000004
total: $59.97
each pays 1499 cents
left over: 1 cent
0.30000000000000004
How It Works
Store cents, not dollars.1999n means $19.99. Multiplying by 3n gives exactly 5997n cents, with no rounding error possible.
Split with / and %./ 100n gives the whole dollars and % 100n the remaining cents; padStart(2, "0") keeps a cent value like 7 as 07. Dividing by 4 people gives 1499 cents each and 1 cent left over.
Compare with floating point. The last line shows why: 0.1 + 0.2 with Numbers prints 0.30000000000000004, because 0.1 has no exact binary form.
Continue
BigInt Method Groups
BigInt has five methods. Each one has a full tutorial with examples you can run.
BigInt is a primitive type for whole numbers of any size. You create one with an n suffix, like 123n, or with BigInt(123), and typeof gives "bigint".
Number.MAX_SAFE_INTEGER, which is 9007199254740991 (2 to the power of 53, minus 1). Above it a Number can no longer store every whole number exactly, so 9007199254740993 turns into 9007199254740992.
JavaScript refuses to guess which type you want, because either conversion could lose information. 1n + 1 throws a TypeError; write 1n + BigInt(1) or Number(1n) + 1 instead.
No. BigInts are always whole numbers. Division drops the fraction, so 7n / 2n is 3n, and BigInt(1.5) throws a RangeError. For cents, store whole cents as a BigInt.
Usually yes, because a Number is a fixed-size value the processor handles directly. Use BigInt when you need exact whole numbers beyond the safe limit, and plain Numbers everywhere else.