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 .
// problem of large integer in JavaScript console.log( 9007199254740993 === 9007199254740992, ); // true console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2); // true
BigInt solves this by supporting integers with unlimited size (only limited by available memory).
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(1093508046768863039966381565n); // 1093508046768863039966381565n
big int constructor
Basic arithmetic
BigInt supports the usual arithmetic operators.
// plus, add console.log(3n + 2n) // 5n
// minus, substraction 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
🛑 WARNING: you cannot mix BigInt 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
mix big int with normal number
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.log((12345678901234567890n).toString()); // "12345678901234567890" // to hexadecimal console.log((12345678901234567890n).toString(16)); // ab54a98ceb1f0ad2 // format console.log((12345678901234567890n).toLocaleString()); // "12,345,678,901,234,567,890"
Converting between number and bigint
// 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 bigint
// 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.
JavaScript bigint
JavaScript. Number
- JS: Number
- JS: Number Input. Binary, Hexadecimal, Octal, Etc
- JS: BigInt tutorial
- JS: Assignment Operators
- JS: Arithmetic Operators
- JS: Number Comparison Operators
- JS: Convert String and Number
- JS: Convert Decimal, Hexadecimal
- JS: Convert Decimal, Binary
- JS: Format Number
- JS: Random Integer Range Function 📜
- JS: Number (class)
- JS: Number Constructor
- JS: Number.prototype
- JS: Math (namespace)