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.

A numeric ID or timestamp from a backend can exceed JavaScript’s safe integer range. At that point, ordinary number values may no longer represent every integer precisely. BigInt is a separate JavaScript numeric type for integer calculations beyond those limits; it is not a replacement for number in everyday UI calculations. section 2.4 section 2.4.1

Count past the safe limit

Number.MAX_SAFE_INTEGER is 9007199254740991. Convert that safe value with BigInt(Number.MAX_SAFE_INTEGER), then add 1n and 2n: the results are 9007199254740992 and 9007199254740993. You can also create a BigInt by appending n to an integer literal or by passing a decimal string to BigInt(). section 2.4.1

Keep the integer-only rules in mind:

  • Use BigInt values on both sides of arithmetic: 5n + 1 throws a TypeError, while 5n + 1n works. Comparisons may mix number and bigint. section 2.4.2
  • Division discards any fractional part. Most basic arithmetic operators work, but unary + does not. section 2.4.2
  • Keep BigInt behind the scenes when possible: Math methods and JSON.stringify() do not accept it directly, and DOM APIs and CSS expect regular numbers. Ordinary numbers remain the practical choice for layout, styles, and animations. section 2.4.3 section 2.4.4
function isSafeInt(value) {

  if (typeof value === "number") {    #1
      return Number.isSafeInteger(value);    #2
  }

  if (typeof value === "bigint") {    #3
      return value >= BigInt(Number.MIN_SAFE_INTEGER) &&    #4
             value <= BigInt(Number.MAX_SAFE_INTEGER);       #4
  }

  return false;    #5
}

This safe-integer check handles both numeric types. It uses Number.isSafeInteger() for number values, but compares bigint values against bounds converted to BigInt. section 2.8

Use BigInt when precise, potentially out-of-range integers matter. If the values stay within the safe range—or must drive a visual property—stick with number. section 2.4.4

How Math for Web Design covers it

How deep it goes
Works through construction, arithmetic, integer division, mixed-type errors, and API limits without descending into implementation internals.
Examples
Calculates successive integers beyond Number.MAX_SAFE_INTEGER and checks whether number or bigint values fall within the safe range.
What it assumes
Introduces JavaScript’s number and bigint types and shows typeof checks before applying safe-integer bounds.
What you can do after
Build it from scratch: write a mixed-type safe-integer check using typeof, Number.isSafeInteger(), and BigInt bounds.
How it connects
Links BigInt to JavaScript number, safe integers, type checking, arithmetic operators, JSON serialization, and DOM APIs.

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.