BigInt
This lesson explains BigInt with clear examples, use cases, and best practices so you can use modern JavaScript confidently in real projects.
ES6 BigInt Overview
BigInt is a JavaScript primitive data type introduced to
represent integers larger than the maximum safe integer supported by the
Number type.
Before BigInt, JavaScript numbers could safely represent integers only up
to 9007199254740991 (253 − 1). Values larger than
this lose precision. BigInt solves this limitation.
| Feature | Description |
| Type | Primitive |
| Introduced | ES2020 (Modern JavaScript) |
| Purpose | Represent arbitrarily large integers. |
| Decimal Support | No (integers only) |
| Suffix | n |
| Common Uses | Finance, cryptography, scientific calculations, blockchain, large IDs. |
Why BigInt?
JavaScript numbers use the IEEE-754 double-precision format. Large integer
values lose precision after the maximum safe integer.
console.log(
Number.MAX_SAFE_INTEGER
);
// 9007199254740991
const number =
9007199254740992;
console.log(number + 1);
console.log(number + 2);
// Unexpected precision loss
BigInt solves this problem by storing integers with arbitrary precision.
Creating BigInt Values
Append the n suffix to an integer literal.
const value =
123456789012345678901234567890n;
console.log(value);
You can also use the BigInt() constructor.
const id =
BigInt("987654321987654321987654321");
console.log(id);
Checking the Data Type
const total =
500n;
console.log(typeof total);
// bigint
BigInt has its own primitive type named
bigint.
Arithmetic Operators
BigInt supports most arithmetic operators.
const a =
100n;
const b =
25n;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
| Operator | Supported |
| + | Yes |
| - | Yes |
| * | Yes |
| / | Yes |
| % | Yes |
| ** | Yes |
Division Behavior
BigInt division always returns another integer.
console.log(
7n / 2n
);
// 3n
Decimal values are discarded because BigInt only supports integers.
Exponentiation
const value =
2n ** 10n;
console.log(value);
// 1024n
The exponent operator works exactly like Number arithmetic.
Comparing BigInt Values
console.log(
100n == 100
);
// true
console.log(
100n === 100
);
// false
console.log(
20n > 10n
);
// true
Strict equality compares both value and type.
Mixing Number and BigInt
JavaScript does not automatically mix Number and BigInt values.
const price =
100n;
const tax =
20;
// TypeError
console.log(
price + tax
);
Convert one type before performing arithmetic.
const total =
price + BigInt(tax);
console.log(total);
Converting Between Number and BigInt
const number =
Number(500n);
console.log(number);
const bigint =
BigInt(500);
console.log(bigint);
Be careful when converting very large BigInt values into Numbers because
precision may be lost.
Boolean Conversion
console.log(
Boolean(0n)
);
// false
console.log(
Boolean(10n)
);
// true
Like Number values, zero is falsy and non-zero values are truthy.
String Conversion
const accountId =
987654321987654321n;
const text =
accountId.toString();
console.log(text);
Use toString() when displaying BigInt values.
JSON Limitation
JSON does not support BigInt values directly.
const user =
{
id: 1000n
};
JSON.stringify(user);
// TypeError
Convert BigInt values into strings before serializing JSON.
const user =
{
id: "1000"
};
JSON.stringify(user);
Math Object Limitation
Most Math methods do not support BigInt.
Math.sqrt(100n);
// TypeError
Convert to Number if precision loss is acceptable.
Real-World Examples
Blockchain Wallet Balance
const walletBalance =
1250000000000000000000n;
console.log(walletBalance);
Large Database IDs
const recordId =
987654321987654321987654321n;
console.log(recordId);
Financial Systems
const bankReserve =
250000000000000000000000n;
console.log(bankReserve);
Scientific Calculations
const stars =
999999999999999999999999999n;
console.log(stars);
Number vs BigInt
| Feature | Number | BigInt |
| Decimals | Supported | Not Supported |
| Maximum Safe Integer | 253 − 1 | No Limit |
| Precision | Limited | Unlimited Integers |
| Math Object | Supported | Mostly Unsupported |
| JSON Support | Supported | Requires Conversion |
| Performance | Faster | Slightly Slower |
Common BigInt Mistakes
- Mixing Number and BigInt without conversion.
- Expecting decimal values from BigInt division.
- Using BigInt with Math methods.
- Trying to serialize BigInt directly with JSON.
- Using BigInt when normal Number values are sufficient.
- Forgetting the
n suffix. - Using strict equality between Number and BigInt.
- Converting large BigInt values to Number and losing precision.
BigInt Best Practices
- Use BigInt only for very large integers.
- Use Number for decimal calculations.
- Convert values explicitly before arithmetic.
- Store BigInt as strings when exchanging JSON.
- Avoid unnecessary conversions.
- Use strict equality when comparing types.
- Document APIs that return BigInt values.
- Benchmark performance when processing millions of values.
Key Takeaways
- BigInt stores integers larger than Number can safely represent.
- BigInt values use the
n suffix. - BigInt and Number cannot be mixed directly.
- BigInt does not support decimal numbers.
- Use BigInt for IDs, finance, blockchain, and scientific data.
- Convert BigInt to strings before JSON serialization.
Pro Tip
A common interview question is "When should you use BigInt?"
The answer is: use BigInt only when working with integers
larger than Number.MAX_SAFE_INTEGER. For everyday
calculations, decimal values, graphics, and UI logic, continue using the
Number type because it is faster and fully supported by
JavaScript's Math APIs.