JS: BigInt tutorial
What is big int
(new in JS: ECMAScript 2020)
BigInt is a primitive type . It lets you work with integers of arbitrary precision.
The Number type in JavaScript cannot represent integers bigger than Number.MAX_SAFE_INTEGER.
BigInt solves this by supporting integers with unlimited size.
Big int type
console.assert(typeof 123n === "bigint");
Create big int
Literal syntax
// put n at the end of digit sequence, makes it a big int type // this is big int console.log(3n); // 3n console.log(3n == 3); // true
// very big number console.log(12345678901234567890n); // 12345678901234567890n console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
Big int constructor
Basic arithmetic
Big Int supports the usual arithmetic operators.
// plus, add console.log(3n + 2n) // 5n
// minus, subtraction console.log(3n - 2n) // 1n // negation console.log(-(-3n)); // 3n console.log(-3n); // -3n
// multiply console.log(3n * 2n) // 6n
// divide console.log(10n / 2n); // 5n // not divisible. return integer quotient console.log(11n / 5n); // 2n console.log(-11n / 5n); // -2n
// mod, remainder console.log(5n % 2n); // 1n
// power console.log(3n ** 2n); // 9n
Arithmetic operators cannot mix big int type with number type
🛑 warning: arithmetic operators cannot mix big int type with number type.console.log(3n + 2); // error: Uncaught (in promise) TypeError: Cannot mix BigInt and other types, use explicit conversions
Number comparison
console.log(3n < 4n); // true console.log(3n <= 3n); // true // s------------------------------ console.log(3n > 4n); // false console.log(3n >= 3n); // true // s------------------------------ // double equal console.log(3n == 3n); // true console.log(3n != 3n); // false // s------------------------------ // triple equal console.log(3n === 3n); // true console.log(3n !== 3n); // false
Comparison of big int type with number type
console.log(3n < 4); // true console.log(3n <= 3); // true // s------------------------------ console.log(3n > 4); // false console.log(3n >= 3); // true // s------------------------------ // double equal console.log(3n == 3); // true console.log(3n != 3); // false // s------------------------------ // WARNING. triple equal cannot be used. console.log(3n === 3); // false console.log(3n !== 3); // true
Convert to string, hexadecimal, format
// to string console.assert( (12345678901234567890n).toString() === "12345678901234567890", ); // to hexadecimal console.assert( (12345678901234567890n).toString(16) === "ab54a98ceb1f0ad2", ); // format console.assert( (12345678901234567890n).toLocaleString() === "12,345,678,901,234,567,890", );
Convert between number and big int
// bigint to num // lossy if too big console.log(Number(123n)); // 123
// num to bigint console.log(BigInt(123)); // 123n
Convert to number
BigInt.asIntNBigInt.asUintN
JSON does not support big int
// Custom replacer / reviver JSON.stringify({ big: 12345678901234567890n }, (k, v) => typeof v === 'bigint' ? v.toString() + 'n' : v); JSON.parse(json, (k, v) => typeof v === 'string' && v.endsWith('n') ? BigInt(v.slice(0, -1)) : v);
Browser support
supported by all major browsers since 2021.