JavaScript BigInt

Beginner
8 min read
Updated: Nov 2026
3 examples
Big whole numbers

Definition and Usage

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.

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 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:

JavaScript
Number.MAX_SAFE_INTEGER;                 // 9007199254740991
9007199254740993;                        // 9007199254740992 (the last digit changed!)
2 ** 53 === 2 ** 53 + 1;                 // true
Number.isSafeInteger(2 ** 53);           // false
9007199254740993n;                       // 9007199254740993n (exact)
BigInt(Number.MAX_SAFE_INTEGER) + 2n;    // 9007199254740993n

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.

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)

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.

JavaScript
1n == 1;          // true (same value)
1n === 1;         // false (different types)
2n > 1;           // true
1n < 1.5;         // true
Boolean(0n);      // false (0n is falsy, like 0)
[3n, 1n, 10n, 2n].sort().join(",");   // "1,10,2,3" (sorted as text!)

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.

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.

JavaScript
Number(123n);                   // 123
Number(9007199254740993n);      // 9007199254740992 (precision lost)
BigInt(1e21);                   // 1000000000000000000000n
String(2n ** 64n);              // "18446744073709551616"
Math.max(1n, 2n);               // TypeError
Math.sqrt(16n);                 // TypeError
JSON.stringify({ id: 1n });     // TypeError

function bigintReplacer(key, value) {
  return typeof value === "bigint" ? value.toString() : value;
}
JSON.stringify({ id: 1n }, bigintReplacer);   // '{"id":"1"}'

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.

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.

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.

JavaScript
console.log(Number.MAX_SAFE_INTEGER);                  // 9007199254740991
console.log(9007199254740993);                         // 9007199254740992
console.log(String(9007199254740993n));                // 9007199254740993
console.log(typeof 9007199254740993n);                 // bigint
console.log(String(BigInt(Number.MAX_SAFE_INTEGER) + 2n));   // 9007199254740993
Try It Yourself

How It Works

  1. The safe limit is 2^53 - 1. Number.MAX_SAFE_INTEGER is the largest whole number a Number can store exactly: 9007199254740991.
  2. 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.
  3. 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.

JavaScript
const big = 10n;

try {
  console.log(big + 1);
} catch (e) {
  console.log(e.name);                   // TypeError
}

console.log(String(big + BigInt(1)));    // 11
console.log(Number(big) + 1);            // 11
console.log(String(7n / 2n));            // 3
console.log(1n == 1);                    // true
console.log(1n === 1);                   // false
console.log(2n > 1);                     // true
Try It Yourself

How It Works

  1. Mixing types throws. big + 1 adds a BigInt and a Number, so JavaScript throws a TypeError instead of guessing which type you meant.
  2. 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.
  3. 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
Try It Yourself

How It Works

  1. Store cents, not dollars. 1999n means $19.99. Multiplying by 3n gives exactly 5997n cents, with no rounding error possible.
  2. 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.
  3. 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.

BigInt Method Groups

BigInt has five methods. Each one has a full tutorial with examples you can run.

GroupMethodsUse them to
Static methodsBigInt.asIntN(), BigInt.asUintN()imitate fixed-size integers such as int8, uint32, or int64, with signed or unsigned wrap-around
Instance methodstoString(), toLocaleString(), valueOf()print a BigInt as digits, hex, binary, or locale-formatted text, or unwrap a wrapper object

Full list with one-line descriptions: BigInt Methods index.

Frequently Asked Questions

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.

Browse every BigInt method

Open the index for asIntN(), asUintN(), toString(), toLocaleString(), and valueOf().

BigInt Methods index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful