JavaScript BigInt
BigInt is a built-in JavaScript type that allows you to represent integers of arbitrary size. Regular JavaScript numbers (Number type) can only safely represent integers up to 2^53 - 1. BigInt removes this limitation, making it possible to work with very large integers precisely.
Creating BigInt Values
There are two ways to create a BigInt: append 'n' to an integer literal, or use the BigInt() constructor function. BigInt values are a distinct type from Number.
// Using the 'n' suffix
let big1 = 123456789012345678901234567890n;
console.log(big1);
console.log(typeof big1);
// Using BigInt() constructor
let big2 = BigInt("123456789012345678901234567890");
console.log(big2);
// From regular number (must be safe integer)
let big3 = BigInt(42);
console.log(big3);
// Compare with regular Number precision loss
let regular = 9007199254740993;
let bigVersion = 9007199254740993n;
console.log(regular);
console.log(bigVersion);BigInt Operations
BigInt supports standard arithmetic operators: +, -, *, /, %, and **. Division truncates toward zero (no decimals). You cannot mix BigInt and Number in operations.
let a = 100n;
let b = 30n;
console.log(a + b);
console.log(a - b);
console.log(a * b);
// Division truncates (no decimals)
console.log(a / b);
console.log(7n / 2n);
// Modulo
console.log(a % b);
// Exponentiation
console.log(2n ** 64n);
// Very large calculations
let factorial20 = 1n;
for (let i = 1n; i <= 20n; i++) {
factorial20 *= i;
}
console.log("20! =", factorial20);BigInt Limitations
BigInt has several limitations: it cannot be mixed with Number in operations, it does not support Math methods, it cannot represent decimals, and unary + is not supported.
// Cannot mix BigInt and Number
try {
let result = 10n + 5;
} catch (e) {
console.log("Error:", e.message);
}
// Must convert explicitly
let big = 10n;
let num = 5;
console.log(big + BigInt(num));
console.log(Number(big) + num);
// No Math methods
try {
console.log(Math.sqrt(4n));
} catch (e) {
console.log("Math error:", e.message);
}
// No decimals
console.log(5n / 2n);
// Unary + not supported
try {
console.log(+10n);
} catch (e) {
console.log("Unary+ error:", e.message);
}Comparing BigInt and Number
BigInt and Number can be compared using ==, <, >, but not with strict equality (===) since they are different types. BigInt values can be sorted alongside Numbers in arrays.
// Loose equality works across types
console.log(10n == 10);
console.log(10n === 10);
// Comparison operators work
console.log(10n > 5);
console.log(10n < 20);
console.log(5n >= 5);
// Sorting mixed arrays
let mixed = [4n, 6, 2n, 5, 3n, 1];
mixed.sort((a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
console.log(mixed);
// Boolean behavior
console.log(Boolean(0n));
console.log(Boolean(1n));
if (0n) { console.log("truthy"); } else { console.log("0n is falsy"); }When to Use BigInt
Use BigInt when you need to work with integers larger than Number.MAX_SAFE_INTEGER, such as database IDs, cryptographic calculations, high-precision timestamps, or financial calculations requiring large integers.
// Large ID handling (e.g., Twitter/Snowflake IDs)
let tweetId = 1234567890123456789n;
console.log("Tweet ID:", tweetId.toString());
// Precise large integer arithmetic
let a = 9007199254740993n;
let b = 9007199254740994n;
console.log("Are they different?", a !== b);
console.log("Difference:", b - a);
// Convert for JSON (BigInt is not JSON-serializable)
let data = { id: 123456789n, name: "test" };
let json = JSON.stringify(data, (key, value) =>
typeof value === "bigint" ? value.toString() : value
);
console.log(json);| Feature | Number | BigInt |
|---|---|---|
| Max safe integer | 2^53 - 1 | Unlimited |
| Decimal support | Yes | No |
| Math methods | Yes | No |
| JSON.stringify | Yes | No (needs custom) |
| typeof | "number" | "bigint" |
| Literal syntax | 42 | 42n |
| Unary + | Supported | Not supported |