liveBook · Manning

Arbitrary-precision integer

As explained in Math for Web Design

BigInt is a JavaScript primitive numeric type for representing integers of arbitrary magnitude.

BigInt is JavaScript’s integer type for values that may exceed the safe range of the ordinary number type. Use it when backend IDs, timestamps, financial data, scientific APIs, or cryptographic calculations can contain integers larger than Number.MAX_SAFE_INTEGER or smaller than Number.MIN_SAFE_INTEGER. It keeps those integer calculations precise instead of silently losing accuracy. section 2.4 section 2.4.1

Create a BigInt by appending n to an integer literal or by passing an integer string to BigInt(). A BigInt represents integers only: it has no floating-point form. section 2.4.1 section 2.4.2

Listing 2.2 Working with BigInt values
const previousMax = BigInt(Number.MAX_SAFE_INTEGER);
const noLongerUnsafe1 = previousMax + 1n;
const noLongerUnsafe2 = previousMax + 2n;
console.log(`The old maximum safe integer: ${previousMax}`);
console.log(`The old maximum plus 1 equals ${noLongerUnsafe1}`);
console.log(`The old maximum plus 2 equals ${noLongerUnsafe2}`);

Adding beyond the former maximum safe integer continues to produce distinct, exact values when every operand is a BigInt. section 2.4.1

Use BigInt with a few type rules in mind:

  • Use basic arithmetic such as +, -, *, /, %, and **, but do not use unary +. section 2.4.2
  • Do not mix number and bigint in one arithmetic expression; explicitly convert the value you need. section 2.4.2
  • Expect division to discard a fractional part, because BigInt calculations remain integers. section 2.4.2
  • Compare number and bigint values when needed; comparison expressions may contain both types. section 2.4.2

Keep arithmetic in one numeric type

A literal without n is a number, so adding it to a BigInt throws a TypeError. Write both values as BigInts instead: const max = 9007199254740991n; console.log(max + 10n); section 2.4.4 section 2.8

BigInt is mainly for behind-the-scenes calculation, not visual UI values. It is unsupported by Math methods and JSON.stringify(), and it does not automatically work with DOM APIs or CSS. Regular numbers remain the practical choice for animation, layout, styles, serialization, and values that stay within safe integer limits. section 2.4.3 section 2.4.4

How Math for Web Design covers it

How deep it goes
Explains creation, exact arithmetic, type-mixing errors, truncating division, and browser-facing limitations without BigInt internals.
Examples
Uses maximum-safe-integer additions, string construction, mixed-type errors, arithmetic expressions, and an isSafeInt() exercise.
What it assumes
Introduces BigInt from ordinary JavaScript numbers while assuming familiarity with values, operators, and type checks.
What you can do after
Apply it via JavaScript BigInt literals or BigInt() when integer calculations may exceed the safe number range.
How it connects
Connects BigInt with number, Number.MAX_SAFE_INTEGER, arithmetic operators, type conversion, JSON, DOM APIs, and CSS.

Also explained in

Compare how 4 books explain arbitrary-precision integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.