You can represent and compute with whole numbers larger than Number.MAX_SAFE_INTEGER using BigInt literals and the BigInt() function.
What it is
BigInt is a primitive type for arbitrary-precision integers. JavaScript Number stores values as IEEE 754 doubles, so the largest integer that can be represented safely is 2^53 - 1, which is 9007199254740991. Beyond that limit, integers may silently lose precision. BigInt can represent whole numbers of any size, limited only by available memory. Related terms include Number.MAX_SAFE_INTEGER, Number.isSafeInteger(), the n literal suffix, the BigInt() constructor, and mixed BigInt/Number operations.
Why it matters
- Exact IDs from databases or APIs that exceed the safe integer range.
- Financial or scientific calculations that require exact integer arithmetic.
- Bitwise operations on very large integers.
- Counting or indexing huge collections without silent rounding.
- Interoperability with APIs that expose 64-bit integer values.
Syntax or steps
- Create a BigInt by appending
nto an integer literal. - Create a BigInt from a string or Number with
BigInt(value). - Use normal arithmetic operators such as
+,-,*,/,%, and**with BigInt operands. - Convert explicitly when mixing BigInt and Number.
const big = 9007199254740993n;
const fromString = BigInt("9007199254740993");
const sum = big + 1n;
Example
const safeLimit = Number.MAX_SAFE_INTEGER; // 9007199254740991
const tooBig = 9007199254740993n;
console.log(tooBig + 1n); // 9007199254740994n
console.log(tooBig * 2n); // 18014398509481986n
console.log(tooBig / 2n); // 4503599627370496n
console.log(tooBig % 2n); // 1n
console.log(tooBig > safeLimit); // true
console.log(Number(tooBig)); // 9007199254740992 (precision lost)
In this example, 9007199254740993n is a BigInt literal. The operators +, *, /, and % produce BigInt results when both operands are BigInt. Division truncates toward zero, so tooBig / 2n drops the fractional part. Comparing a BigInt with a Number is allowed, but converting a large BigInt to Number can lose precision.
Common mistakes
- Mixing BigInt and Number in arithmetic:
1n + 1throws a TypeError. Fix it by converting withBigInt(1)orNumber(1n)only when safe. - Using BigInt for decimals:
BigInt(1.5)throws a RangeError. Fix it by scaling values, such as storing cents as BigInt. - Assuming JSON supports BigInt:
JSON.stringify({ big: 1n })throws. Fix it by serializing BigInt values as strings and parsing them withBigInt(). - Forgetting the
nsuffix:9007199254740993is a Number and may round. Fix it by writing9007199254740993norBigInt("9007199254740993").
When to use it
Use BigInt when you need exact integers beyond the safe integer range. Use Number for ordinary values, decimals, and performance-sensitive code.
| Need | Use | Why |
|---|---|---|
IDs larger than 2^53 - 1 | BigInt | Exact integer representation |
| Money with cents | Number or scaled BigInt | Number is fine below the safe limit; BigInt avoids float errors when exactness matters |
Decimals like 0.1 | Number | BigInt only stores integers |
| High-performance loops | Number | BigInt operations are generally slower |
Practice
Guided exercise: create a BigInt from the string "12345678901234567890", add 1n, and log the result.
const value = BigInt("12345678901234567890");
console.log(value + 1n); // 12345678901234567891n
Challenge: compute 2n ** 64n and compare it with Number.MAX_SAFE_INTEGER. Hint: use console.log() and a relational operator.
const big = 2n ** 64n;
console.log(big > Number.MAX_SAFE_INTEGER); // true
Quick check
Question: Why does 9007199254740993n + 1n work exactly, but 9007199254740993 + 1 may not give the exact expected integer?
Answer: The first uses BigInt, which stores exact arbitrary-precision integers. The second uses Number, whose safe integer range ends at 9007199254740991, so larger integers can be rounded.
Summary
BigInt gives JavaScript exact whole-number arithmetic beyond the safe integer limit. Use it for large IDs, exact integer math, and values that must not silently round, while keeping Number for decimals and ordinary performance-sensitive calculations.