`BigInt` and Arbitrary-Precision Integers

Intermediate · 19 min

Why `Number` loses precision past `MAX_SAFE_INTEGER`, the `BigInt` primitive and `123n` syntax, what can't mix with `Number`, and when it's overkill.

Before thisValues, Types, and CoercionDates, Numbers, and Internationalization

Good Morning Developers · 19 min

`BigInt` and Arbitrary-Precision Integers

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Good Morning DevelopersReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Every JavaScript `Number` is a 64-bit floating-point value, which can only represent integers *exactly* up to `Number.MAX_SAFE_INTEGER` (2^53 − 1); past that point, distinct integers start rounding to the same stored value, so `Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2` is `true`.
  • `BigInt` is its own primitive type, not a `Number` configured differently — it represents integers of arbitrary size exactly, with no upper bound beyond available memory, created either with a trailing `n` on a literal (`123n`) or by calling `BigInt(123)`.
  • `BigInt` and `Number` cannot be mixed in arithmetic — `1n + 1` throws a `TypeError` — because any implicit conversion between them risks silently losing precision in one direction or the other, and the language refuses to guess which side you meant to trust.
  • Comparison operators are the one place `BigInt` and `Number` *do* mix safely: `1n === 1` is `false` (different types, strict equality cares), but `1n == 1` and `1n < 2` both work, because comparing magnitude doesn't require picking a shared representation the way arithmetic does.
  • Reach for `BigInt` when a value's identity depends on exactness past 2^53 — large database/snowflake IDs, cryptographic values, exact integer math — and skip it everywhere else; it's slower than `Number` for ordinary arithmetic and can't be used with `Math` methods or passed to most APIs expecting a plain number.

The problem: Number quietly stops being exact

JavaScript has one numeric type for ordinary numbers — Number — and it’s a 64-bit IEEE-754 floating-point value, the same representation most languages call a “double.” That format is what gave you 0.1 + 0.2 !== 0.3 back in the lesson on dates and numbers: most decimal fractions don’t have an exact binary representation. Integers have the opposite problem. Small and medium integers are represented exactly — the trouble only starts once you get large enough that the format runs out of bits to keep every integer distinct.

That boundary has a name: Number.MAX_SAFE_INTEGER, equal to 2^53 − 1 (9007199254740991). Below it, every integer has its own exact floating-point value. Above it, the gaps between representable values grow past 1, so distinct mathematical integers start rounding onto the same stored number:

Number.MAX_SAFE_INTEGER;          // 9007199254740991
Number.MAX_SAFE_INTEGER + 1;      // 9007199254740992
Number.MAX_SAFE_INTEGER + 2;      // 9007199254740992 — same value!

Two different numbers you asked for just collapsed into one. That’s not a rounding error you can tune away — it’s a structural limit of the format Number is built on, and it bites hardest with things like large database IDs, where two distinct records could silently end up carrying “the same” ID once stored as a Number.

BigInt: a second, separate numeric primitive

BigInt isn’t a bigger Number or a special mode of it — it’s its own primitive type, built specifically to represent integers of any size exactly, limited only by how much memory the engine is willing to spend. You write a BigInt literal by appending n to an integer, or convert an existing value with the BigInt() function:

const big = 9007199254740993n;       // literal syntax — exact, no rounding
const fromNumber = BigInt(42);       // 42n
const fromString = BigInt("123456789123456789123456789"); // exact, arbitrarily large

Arithmetic between two BigInts works exactly the way you’d expect, with no precision ceiling: +, -, *, / (integer division — no fractional remainder, since BigInt only represents whole numbers), %, and ** all behave correctly at any size.

What can’t mix, and the one thing that can

Here’s the rule that trips people up first: you cannot combine a BigInt and a Number in arithmetic. 1n + 1 throws a TypeError, not a silent coercion. That’s deliberate — converting the BigInt down risks losing precision above Number.MAX_SAFE_INTEGER, and converting the Number up risks silently dropping a fractional part. Rather than pick a side and guess, JavaScript refuses outright and makes you convert explicitly, putting the precision tradeoff in your hands instead of hiding it:

1n + BigInt(1); // 2n — works, both sides are BigInt now
Number(1n) + 1; // 2 — works, both sides are Number now
1n + 1;          // TypeError: Cannot mix BigInt and other types

Comparisons are the exception. <, >, and loose equality == work across the two types directly, because comparing magnitude doesn’t force the engine to settle on one shared representation the way producing a new arithmetic result does:

1n < 2;   // true
1n == 1;  // true  — loose equality allows the type coercion
1n === 1; // false — strict equality also checks type, and they differ

The video covers exactly this territory — why BigInt exists, walking through the documentation and the interop rules, and then grounding it in a real example by solving a large-number problem (Project Euler’s problem 20, which needs the exact digits of a huge factorial) where Number would have silently given a wrong answer. Seeing it solve a problem Number genuinely can’t is a better argument for BigInt than any list of rules.

When it’s worth it, and when it’s noise

The honest advice is: reach for BigInt only when correctness past 2^53 is actually load-bearing. Large, opaque IDs (many distributed systems mint 64-bit “snowflake” IDs), cryptographic computations, and exact large-integer math are real cases. Almost everything else — loop counters, prices, array indices, UI state — never gets remotely close to that boundary, and BigInt there is pure overhead: it’s slower than Number for ordinary arithmetic, can’t be passed to Math methods, can’t hold a fraction, and forces explicit conversions anywhere a plain Number was expected. Using it by default is solving a problem you don’t have while creating one you now do.

Where this fits

This closes out the language’s numeric story — dates, numbers, and internationalization covered Number’s floating-point behavior and its everyday rough edges; BigInt is what you reach for on the rare occasion Number’s precision ceiling itself is the problem, not just its decimal rounding. Combined with values, types, and coercion, you now have the complete picture of every primitive type JavaScript actually has.

Check yourself

  1. 01What does `Number.MAX_SAFE_INTEGER` actually guarantee, and what happens if you go past it?

    It's the largest integer JavaScript's `Number` type can represent where every integer up to that point has its own exact, distinct floating-point representation. Past it, the gaps between representable values grow larger than 1, so multiple different mathematical integers round to the identical stored `Number` — `Number.MAX_SAFE_INTEGER + 1` and `Number.MAX_SAFE_INTEGER + 2` are both `9007199254740992`, the same value, even though they're different numbers.

  2. 02Why does `1n + 1` throw a `TypeError` instead of JavaScript just converting one side to match the other?

    Converting the `BigInt` down to a `Number` could silently lose precision for a value past `Number.MAX_SAFE_INTEGER`, and converting the `Number` up to a `BigInt` would silently discard any fractional part. Either direction can quietly throw away information the programmer probably cares about, so instead of guessing, the language refuses the mixed operation outright and makes you convert explicitly with `BigInt(n)` or `Number(b)`, acknowledging the tradeoff yourself.

  3. 03`5n === 5` is `false`, but `5n == 5` is `true`. What explains the difference?

    `===` checks type as well as value, and `5n` (a `bigint`) and `5` (a `number`) are different types, so strict equality is false regardless of their numeric value. `==` allows type coercion for the comparison, and comparing magnitude between a `BigInt` and a `Number` doesn't carry the same precision-loss risk that *arithmetic* between them does, so the loose comparison operators (and `<`, `>`) are allowed to work across the two types directly.

  4. 04Give a realistic example of when `BigInt` is the right tool, and one where reaching for it would be overkill?

    Right tool: a system assigning 64-bit snowflake-style IDs (common in distributed systems and some APIs) where the ID itself needs to round-trip exactly — storing it as a `Number` risks two different IDs colliding onto the same floating-point value past 2^53. Overkill: counting items in a shopping cart, a loop index, or almost any everyday arithmetic that will never get near 9 quadrillion — `Number` is faster, works with `Math` and most APIs, and using `BigInt` there adds friction (no mixing with `Number`, no decimals) for no real benefit.

  5. 05Why can't you use `Math.sqrt()` or similar `Math` methods directly on a `BigInt`?

    `Math` methods are specified to operate on `Number` values, and `BigInt` is a distinct primitive type that isn't implicitly converted for them the way it isn't implicitly converted in arithmetic — the same precision-loss concern applies. You'd have to explicitly convert the `BigInt` to a `Number` first (accepting whatever precision loss that implies for very large values) to use it with `Math`.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark