
Kar started this conversation 4 weeks ago.
How and why should you use BigInt in JavaScript? What pitfalls do you need to avoid, especially when mixing with Number?
When large integers exceed 2⁵³ −1 or exact integer identity matters (e.g. ID tracking, timestamp ms), a Number may fail. This answer helps explain BigInt usage and gotchas.
Digiaru
Posted 4 weeks ago
A: • JavaScript Number can only safely represent integers up to ±9,007,199,254,740,991 (≈2⁵³ − 1). Numbers beyond this become imprecise. • BigInt enables representing arbitrarily large integers: js Copy code const big = 1234567890123456789012345n; const also = BigInt("1234567890123456789012345"); BigInt is a separate numeric type; typeof big === "bigint". • Do NOT mix BigInt and Number in an operation directly: js Copy code 10n + 5; // TypeError You must convert one side explicitly: js Copy code Number(big) + 5; // returns Number big + 5n; // returns BigInt • Division truncates (floor toward zero) in BigInt: js Copy code 5n / 2n; // 2n • Some operations (like shift >>>, Math library, or Number.isNaN, Math.sqrt) do not support BigInt. Avoid mixing types in APIs expecting standard Number. Use cases: high-resolution timestamps, large IDs, crypto key math, large counters—when integer exactness matters. Tags: JavaScript, BigInt, integer precision, numeric overflow, type conversion