Skip to main content

What is BigInt?

What is BigInt

BigInt is a special data type in JavaScript designed for working with very large integers that go beyond the limits of the standard Number type.

Regular numbers (Number) in JS are stored as 64-bit IEEE-754 values, and the maximum safe integer that can be represented exactly is:

javascript
Number.MAX_SAFE_INTEGER; // 9007199254740991 (≈ 9 * 10^15)

If you exceed this limit, JavaScript starts losing precision:

javascript
console.log(9007199254740991 + 1); // 9007199254740992 (correct) console.log(9007199254740991 + 2); // 9007199254740992 (wrong!)

BigInt solves this problem

BigInt lets you work with integers of any length, without losing precision:

javascript
const big = 9007199254740991n; // a BigInt literal (note the 'n' at the end) console.log(big + 2n); // 9007199254740993n

The n suffix at the end of a number makes it a BigInt. You can also create one through the constructor:

javascript
const big = BigInt("900719925474099312345678901234567890");

Main properties of BigInt

FeatureExample
Created with n or BigInt()123n or BigInt(123)
Supports arithmetic (+, -, *, /, %)100n + 200n → 300n
Division is integer only5n / 2n → 2n (the remainder is dropped)
Cannot be mixed with regular numbers (Number) directly1n + 1 throws an error
Can be converted explicitlyNumber(1n) or BigInt(1)
Works with comparison operators2n > 1n
Works with ===, but is not equal to Number1n === 1 → false

Example of an error when mixing types

javascript
const big = 10n; const num = 5; console.log(big + num); // TypeError

Correct:

javascript
console.log(big + BigInt(num)); // 15n

or

javascript
console.log(Number(big) + num); // 15

Usage examples

  1. Cryptography, hashes, large keys
javascript
const key = 1234567890123456789012345678901234567890n; const mod = 99999999999999999999999999999999999n; console.log(key % mod);
  1. Financial calculations that need precision
javascript
const price = 10000000000000000000n; // an exact sum with no rounding errors
  1. Working with very large counters
javascript
let counter = 0n; for (let i = 0n; i < 1_000_000_000_000_000_000n; i++) { counter++; }

Checking the type

javascript
typeof 123n; // "bigint"

Summary

Key factValue
Data typebigint
What it's forWorking with integers > Number.MAX_SAFE_INTEGER
Creation123n or BigInt(123)
FeatureFull precision, integers only
Incompatible with Numberyou have to convert types manually
DivisionInteger only

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.