BigInt
Added in ves2020 · Updated March 13, 2026 · Built-in Objects
javascript bigint es2020
BigInt is a built-in object that represents arbitrary-precision integers. It allows you to work with integers larger than the safe integer limit (Number.MAX_SAFE_INTEGER, which is 2^53 - 1).
Creating BigInt Values
Create a BigInt by appending n to an integer literal, or by calling the BigInt() function:
const bigNumber = 9007199254740993n;
// 9007199254740993n
const fromString = BigInt("9007199254740993");
// 9007199254740993n
const fromNumber = BigInt(9007199254740993);
// 9007199254740993n
Arithmetic Operations
BigInt supports the standard arithmetic operators:
const a = 10n;
const b = 5n;
a + b // 15n
a - b // 5n
a * b // 50n
a / b // 2n
a % b // 0n
a ** b // 100000n (exponentiation)
The unary minus operator works with BigInt:
-5n // -5n
Comparison Operations
BigInt values can be compared with other BigInt or Number values:
5n > 3 // true
5n === 5 // false (strict equality: different type)
5n == 5 // true (loose equality)
0n === 0n // true
Use BigInt.asUintN() and BigInt.asIntN() to clamp values to a specific bit width:
BigInt.asUintN(8, 300n) // 44n (300 mod 256)
BigInt.asIntN(8, 300n) // 44n
BigInt.asIntN(8, 130n) // -126n (130 - 256)
Type Checking
Check if a value is a BigInt using typeof:
typeof 10n // "bigint"
Limitations
- Cannot mix BigInt with Number in arithmetic operations
- Cannot use
Mathfunctions with BigInt values BigInt()without arguments returns0n
Practical Examples
Working with Large Numbers
// Calculate factorial of 50
function factorial(n) {
if (n <= 1n) return 1n;
return n * factorial(n - 1n);
}
const result = factorial(50n);
// 30414093201713378043612608166064768844377641568960512000000000000n
Bitwise Operations
const value = 15n;
value >> 2n // 3n
value << 2n // 60n
(value | 1n) // 15n (already odd)
See Also
- Number.prototype.toString() — Convert numbers to strings
- Number.isInteger() — Check if a value is an integer
- Number.isFinite() — Check if a value is finite