How to use JavaScript’s BigInt
Blog post from LogRocket
Before ES2020, JavaScript lacked a native method for accurately handling large numbers beyond 9,007,199,254,740,991, leading developers to rely on external libraries like JSBI and bignumber.js. The introduction of BigInt, a numerical data type allowing the representation and manipulation of integers beyond the Number type's safe range, resolved this issue. BigInt is particularly useful in fields that require precise calculations involving large integers, such as finance, cryptography, gaming, and distributed systems. Unlike the Number type, BigInt does not support decimals, preventing round-off errors and ensuring high precision. BigInt operations generally yield accurate results, except for division, which truncates outcomes. BigInt's features include the ability to define integers with n or the BigInt() constructor and methods like BigInt.prototype.toString(), BigInt.asIntN(), and BigInt.asUintN() for various conversions and constraints. Despite some limitations, like incompatibility with built-in Math functions and JSON.stringify(), BigInt significantly enhances JavaScript's capability to handle large numbers efficiently, supported by most modern browsers and Node.js versions from v10.4 onwards.