BigInt
BigInt
Section titled “BigInt”Introduction
Section titled “Introduction”BigInt is a numeric primitive that can represent integers larger than 2^53 - 1 (the maximum safe integer for Number).
Why Do We Need This?
Section titled “Why Do We Need This?”Financial calculations, large IDs, and cryptographic operations often need numbers beyond Number.MAX_SAFE_INTEGER (9,007,199,254,740,991).
Creating BigInt
Section titled “Creating BigInt”// Using n suffixconst big1 = 1234567890123456789012345678901234567890n;
// Using BigInt() functionconst big2 = BigInt('1234567890123456789012345678901234567890');const big3 = BigInt(42); // 42n
console.log(typeof big1); // 'bigint'Operations
Section titled “Operations”const a = 10n;const b = 3n;
a + b; // 13na - b; // 7na * b; // 30na ** b; // 1000n
// Division truncates towards zeroa / b; // 3n (not 3.333...)
// Cannot mix with regular numbers10n + 5; // TypeError!10n + BigInt(5); // 15n ✅Number(10n) + 5; // 15 ✅Comparison
Section titled “Comparison”10n === 10; // false (different types)10n == 10; // true (loose equality works)10n > 5; // true10n < 20; // trueSummary
Section titled “Summary”nsuffix creates BigInt literals- Represents arbitrarily large integers
- Cannot mix with regular
Numberin operations - Use
BigInt()for conversion - Perfect for large IDs, financial calculations