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); // 9007199254740993nThe 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
| Feature | Example |
|---|---|
Created with n or BigInt() | 123n or BigInt(123) |
Supports arithmetic (+, -, *, /, %) | 100n + 200n → 300n |
| Division is integer only | 5n / 2n → 2n (the remainder is dropped) |
Cannot be mixed with regular numbers (Number) directly | 1n + 1 throws an error |
| Can be converted explicitly | Number(1n) or BigInt(1) |
| Works with comparison operators | 2n > 1n |
Works with ===, but is not equal to Number | 1n === 1 → false |
Example of an error when mixing types
javascript
const big = 10n;
const num = 5;
console.log(big + num); // TypeErrorCorrect:
javascript
console.log(big + BigInt(num)); // 15nor
javascript
console.log(Number(big) + num); // 15Usage examples
- Cryptography, hashes, large keys
javascript
const key = 1234567890123456789012345678901234567890n;
const mod = 99999999999999999999999999999999999n;
console.log(key % mod);- Financial calculations that need precision
javascript
const price = 10000000000000000000n; // an exact sum with no rounding errors- 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 fact | Value |
|---|---|
| Data type | bigint |
| What it's for | Working with integers > Number.MAX_SAFE_INTEGER |
| Creation | 123n or BigInt(123) |
| Feature | Full precision, integers only |
| Incompatible with Number | you have to convert types manually |
| Division | Integer only |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.