Chapter III · Data Types

BigInt

BigInt is a built-in numeric primitive used for safely representing integers of arbitrary length beyond the standard number limit.

01 What is BigInt?

BigInt is a primitive type designed to represent integers larger than 2^53 - 1, which is the maximum safe integer limit for standard Numbers.

You create a BigInt by appending an n to the end of an integer literal or by calling the BigInt() function.

02 Simple Example

Working with extremely large integers without precision loss.

bigint.js
const maxSafe = Number.MAX_SAFE_INTEGER;
console.log(maxSafe + 1 === maxSafe + 2); // true (precision loss!)

const huge = 9007199254740991n;
const evenHuger = huge + 2n;
console.log(evenHuger); // 9007199254740993n

BigInts can grow indefinitely in length, making them ideal for cryptography, high-precision IDs, and timestamp calculations.

03 How It Works

BigInts cannot be mixed with standard Numbers in direct arithmetic operations; attempting to add a Number and a BigInt throws a TypeError. Explicit conversion is required.

Variable Reference Primitive Value

BigInt allocates variable-length memory to store arbitrarily large whole numbers safely.

04 Step-by-Step

  1. An integer literal with an n suffix is evaluated.

  2. The engine allocates arbitrary-precision integer storage.

  3. Operations between BigInts execute with exact precision without rounding.

05 Mental Model

"An expanding digital abacus that adds extra columns as numbers grow infinitely larger."

It never runs out of digit slots, unlike fixed-width numeric registers.

06 Interview Questions

Q: Can you mix BigInt and Number in the same expression?

No — 1n + 1 throws a TypeError. You must explicitly convert one side (Number(bigintValue) or BigInt(numberValue)).

Q: When would you actually need BigInt?

Cryptography, high-precision timestamps, or any integer math that could exceed 2^53 — rare in typical UI code, common in specialized numeric work.

Key Takeaway

BigInt enables safe calculation of arbitrarily large integers, preventing the precision loss inherent in standard JavaScript numbers.

Related Concepts