BigInt
BigInt is a built-in JavaScript primitive type introduced in ECMAScript 2020 to represent arbitrary-precision integers.
Unlike the Number
type, which is limited to 64-bit floating-point values and has a safe integer range from Number.MIN_SAFE_INTEGER (-2^53 + 1)
to Number.MAX_SAFE_INTEGER (2^53 - 1)
, BigInt can handle integers of any size without precision loss.
It is especially useful in cryptography, financial applications, and unique identifier generation.
Creating BigInt Values
Using the BigInt() Constructor
Converts a number, string, or boolean into a BigInt.
console.log(BigInt(123)); // 123n
console.log(BigInt("9007199254740992")); // 9007199254740992n
console.log(BigInt(true)); // 1n
Using the n
Suffix
Appending n
to an integer literal creates a BigInt directly.
console.log(123n); // 123n
console.log(9007199254740992n); // 9007199254740992n
Operations with BigInt
BigInt supports arithmetic, comparison, and bitwise operations — but only with other BigInt values.
Arithmetic Operators
Supported operators: +
, -
, *
, /
, %
, **
let a = 9007199254740991n;
let b = 2n;
console.log(a + b); // 9007199254740993n
console.log(a * b); // 18014398509481982n
console.log(a ** b); // 81129638414606663681390495662081n
Division
Division always truncates toward zero (no fractional results).
console.log(10n / 3n); // 3n
console.log(10n % 3n); // 1n
Comparison Operators
BigInt and Number can be compared, but strict equality checks type as well.
console.log(123n === 123); // false
console.log(123n == 123); // true
console.log(123n > 100); // true
Bitwise Operators
Supported: &
, |
, ^
, ~
, <<
, >>
console.log(123n & 456n); // 72n
BigInt and Math Methods
Most Math
methods do not support BigInt, as they are designed for floating-point Numbers.
Example
console.log(Math.sqrt(16n)); // TypeError
console.log(Math.random() * 10n); // TypeError
Workaround
Convert to Number
when within safe limits:
let big = 16n;
if (big <= BigInt(Number.MAX_SAFE_INTEGER)) {
console.log(Math.sqrt(Number(big))); // 4
}
Random BigInt Example
Since Math.random()
returns a Number
, custom logic is required:
function getRandomBigInt(min, max) {
min = BigInt(min);
max = BigInt(max);
let range = max - min + 1n;
let random = BigInt(Math.floor(Math.random() * Number(range)));
return min + random;
}
console.log(getRandomBigInt(1n, 10n)); // e.g., 7n
BigInt and Number Object
Type Checking
console.log(typeof 123n); // 'bigint'
console.log(typeof 123); // 'number'
console.log(Number.isNaN(123n)); // false
console.log(Number.isFinite(Number(123n))); // true
Conversion Between BigInt and Number
To Number:
let big = 123n;
console.log(Number(big)); // 123
console.log(+big); // 123
To BigInt:
console.log(BigInt(123)); // 123n
Converting large BigInt to Number can cause precision loss:
let large = 9007199254740992n;
console.log(Number(large) === Number(large + 1n)); // true
Safe conversion:
function safeToNumber(big) {
if (big <= BigInt(Number.MAX_SAFE_INTEGER) && big >= BigInt(Number.MIN_SAFE_INTEGER)) {
return Number(big);
}
return null;
}
Formatting:
let big = 123456789n;
console.log(Number(big).toLocaleString()); // '123,456,789'
Common Pitfalls with BigInt
Mixing BigInt and Number
console.log(123n + 123); // TypeError
console.log(123n + BigInt(123)); // 246n
console.log(Number(123n) + 123); // 246
Using BigInt with Math Methods
console.log(Math.random() * 100n); // TypeError
Precision Loss
let big = 9007199254740992n;
console.log(Number(big + 1n) === Number(big)); // true
No Floating-Point Support
console.log(BigInt(12.34)); // RangeError
console.log(BigInt(Math.floor(12.34))); // 12n
String Formatting Limitations
console.log(123n.toFixed(2)); // TypeError
console.log(Number(123n).toFixed(2)); // '123.00'