liveBook · Manning

Arbitrary-precision integer

As explained in JavaScript in Depth

An integer value that can grow beyond fixed machine word limits without overflow in the represented range.

A database can hold a 64-bit unsigned ID larger than JavaScript’s maximum safe Number integer, 9,007,199,254,740,991. Reading that ID as a Number risks silently changing it. BigInt is JavaScript’s integer type for values beyond that boundary: it uses memory as needed rather than a fixed-width floating-point representation, preserving exact integer values. section 3.1.1 section 3.2

The first unsafe integer

In JavaScript, `9007199254740993` is a Number and does not retain that exact value; `9007199254740993n` is a BigInt and does. The `n` suffix matters before arithmetic begins: converting an already imprecise Number cannot recover the lost integer. BigInt also supports values such as `10n ** 100n`. section 3.2

Arbitrary precision describes the value’s ability to grow beyond a fixed machine word, not a required storage layout. ECMAScript leaves BigInt representation largely to implementations. V8 stores a large value across 64-bit chunks, reconstructing it by shifting higher chunks and adding lower ones; it keeps the sign separately. Growth therefore consumes more memory. section 3.2 section 3.5

Table 3.6 BigInt storage as 64-bit chunks
Chunk
Value
Binary (partial)
Purpose
0
3,584,949,746
...11010101110010
Lower 64-bits
1
6,692,605,942
...10001111110110
Higher 64-bits

The chunk table illustrates why a large BigInt need not fit in one 64-bit integer: its lower and higher portions occupy separate storage. section 3.2

Keep numeric types deliberate. JavaScript forbids arithmetic that mixes BigInt and Number, although comparisons work; Math methods operate on Numbers only. BigInt also addresses exact *integers*, not decimal fractions such as `0.1`: the proposed Decimal type targets exact base-10 arithmetic. Exactness has a cost—the stated BigInt performance penalty is 10–200×—so it is not a default replacement for ordinary Number arithmetic. section 3.5 section 3.1.1

How JavaScript in Depth covers it

How deep it goes
Explains literals and precision loss, then traces V8’s chunked storage while distinguishing it from implementation-independent language behavior.
Examples
Uses `9007199254740993` versus `9007199254740993n`, a googol calculation, and a table of 64-bit storage chunks.
What it assumes
Builds from JavaScript Numbers and binary integer limits before introducing BigInt literals and storage.
What you can do after
Apply BigInt to preserve large integer values while avoiding mixed-type arithmetic and accounting for its performance cost.
How it connects
Contrasts BigInt with Number’s safe integer limit, IEEE 754 floating-point, proposed Decimal arithmetic, and V8 numeric optimizations.

Also explained in

Compare how 5 books explain arbitrary-precision integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.