JS: BigInt (tutorial)

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2020)

xtodo

js bigint tutorial

(AI generated)

JavaScript BigInt Tutorial

BigInt is a built-in JavaScript primitive type (added in ES2020) that lets you work with integers of arbitrary precision—far beyond the safe integer limit of the regular Number type.

Why BigInt?

The Number type in JavaScript is a 64-bit floating-point value (IEEE 754). It can safely represent integers only up to 2⁵³ - 1 (9,007,199,254,740,991), known as Number.MAX_SAFE_INTEGER.

Beyond that, precision is lost:

console.log(9007199254740992 + 1);   // 9007199254740992  ← same as above!
console.log(9007199254740993 === 9007199254740992); // true

BigInt solves this by supporting integers with unlimited size (only limited by available memory).

1. Creating BigInt Values

There are two ways:

// 1. Literal syntax (recommended)
const big1 = 123456789012345678901234567890n;  // note the 'n' suffix

// 2. BigInt() constructor
const big2 = BigInt("123456789012345678901234567890");
const big3 = BigInt(123);                       // from number (only safe integers)

Important rules:

2. Basic Operations

BigInt supports the usual arithmetic operators, but you cannot mix BigInt with Number directly.

const a = 1000000000000000000n;
const b = 2000000000000000000n;

console.log(a + b);   // 3000000000000000000n
console.log(a - b);   // -1000000000000000000n
console.log(a * b);   // 2000000000000000000000000000000000000n
console.log(a / b);   // 0n          (truncates toward zero)
console.log(a % b);   // 1000000000000000000n
console.log(a ** 2n); // exponentiation works with BigInt

Comparison works across types (but equality is strict):

console.log(1n < 2);     // true
console.log(1n === 1);   // false  ← different types
console.log(1n == 1);    // true   ← loose equality coerces

Unary operators:

-5n          // negative
+5n          // positive (rarely used)

3. Useful Methods

const big = 12345678901234567890n;

big.toString();           // "12345678901234567890"
big.toString(16);         // hexadecimal
big.toLocaleString();     // "12,345,678,901,234,567,890"

BigInt.asIntN(64, big);   // wrap to 64-bit signed integer
BigInt.asUintN(64, big);  // wrap to 64-bit unsigned integer

4. Common Patterns & Examples

Factorial (great BigInt demo)

function factorial(n) {
  let result = 1n;
  for (let i = 2n; i <= n; i++) {
    result *= i;
  }
  return result;
}

console.log(factorial(100n));
// → a huge number: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000n

Cryptography / Large IDs

// Generate a random 256-bit BigInt (common in crypto)
const randomBig = BigInt("0x" + Array.from({length: 64}, () =>
  Math.floor(Math.random() * 16).toString(16)).join(''));

console.log(randomBig);

Converting between Number and BigInt

const num = Number(12345678901234567890n);  // lossy if too big
const big = BigInt(9007199254740991);       // safe

Working with JSON

JSON does not support BigInt natively:

// Custom replacer / reviver
JSON.stringify({ big: 12345678901234567890n }, (k, v) =>
  typeof v === 'bigint' ? v.toString() + 'n' : v);

JSON.parse(json, (k, v) =>
  typeof v === 'string' && v.endsWith('n') ? BigInt(v.slice(0, -1)) : v);

5. BigInt vs Number: Key Differences

Feature Number BigInt
Max safe integer 2⁵³-1 Unlimited
Decimal support Yes (floating point) No (integer only)
Mixed operations Error unless converted
Array indexing Yes No
Bitwise operators 32-bit Full arbitrary precision

All bitwise operators work on BigInt (&, |, ^, ~, <<, >>, >>> is not supported on BigInt).

6. Practical Use Cases

7. Browser/Node Support

BigInt is supported in:

You can safely use it in 2026+ projects.

Quick Reference Cheat Sheet

// Creation
123n
BigInt("123")

// Arithmetic
a + b, a - b, a * b, a / b, a % b, a ** b

// Comparison
a < b, a === b

// Conversion
Number(big)        // careful with size
big.toString()
BigInt(num)        // only safe numbers

// Bitwise (full width)
a & b, a | b, a ^ b, ~a, a << b, a >> b

Pro tip: When writing libraries that should handle both Number and BigInt, check type with typeof:

function add(a, b) {
  if (typeof a === 'bigint' || typeof b === 'bigint') {
    return BigInt(a) + BigInt(b);
  }
  return a + b;
}

That's it! BigInt is simple once you remember the "no mixing with Number" rule and the n suffix. Practice with very large numbers and you'll love it. Happy coding!

JavaScript bigint

JavaScript. Number