BigInt – Working with Large Numbers
In the world of programming, numbers are a fundamental element. In JavaScript, there has long been the Number data type, which, however, has limits due to its implementation as a 64-bit floating-point number (according to the IEEE 754 standard). In particular, integer values can only be represented exactly up to a certain size. With the introduction of BigInt in ES2020, JavaScript now has the ability to safely work with integers of arbitrary size.
Why BigInt?
The Number data type can only represent integers exactly between -(2^53 - 1) and 2^53 - 1 (approximately ±9,007,199,254,740,991). Values outside this range can lead to loss of precision.
Example:
const maxSafeInteger = Number.MAX_SAFE_INTEGER;
console.log(maxSafeInteger); // 9007199254740991
console.log(maxSafeInteger + 1); // 9007199254740992
console.log(maxSafeInteger + 2); // 9007199254740992 // Expected: 9007199254740993
As you can see, adding 1 and 2 results in the same value, which can lead to unexpected behavior.
Numeric Separators
To improve readability, I want to introduce the topic of “Numeric Separators” here. They were introduced in JavaScript in ES12 and greatly increase the readability of numeric values. Here, as we are used to with decimal points (.), underscores (_) are inserted at the appropriate places in a number. However, the browser ignores these underscores. They only increase readability for us.
const a = 123456789;
const b = 123_456_789;
console.log(a === b); // true
Introduction to BigInt
BigInt is a new primitive data type that represents integers of arbitrary size. BigInt values are created by appending an n to the end of the number.
Example:
const bigNumber = 9007199254740992n;
const anotherBig = BigInt("9007199254740992");
console.log(bigNumber); // 9007199254740992n
console.log(anotherBig); // 9007199254740992n
Arithmetic with BigInt
BigInt supports all standard arithmetic operations:
const a = 10n;
const b = 20n;
console.log(a + b); // 30n
console.log(a * b); // 200n
console.log(b - a); // 10n
console.log(b / a); // 2n
console.log(b % a); // 0n
console.log(b ** 2n); // 400n
Important Notes
1. Cannot Mix BigInt and Number:
const bigInt = 10n;
const num = 5;
// console.log(bigInt + num); // TypeError: Cannot mix BigInt and other types
// Convert first:
console.log(bigInt + BigInt(num)); // 15n
console.log(Number(bigInt) + num); // 15
2. Division Truncates:
console.log(5n / 2n); // 2n (not 2.5n)
3. Cannot Use with Math Methods:
// Math.sqrt(4n); // TypeError
4. Comparison Works:
console.log(5n < 10); // true
console.log(5n === 5); // false (different types)
console.log(5n == 5); // true (loose equality)
Practical Use Cases
1. Large Integers:
const largeNumber = 1234567890123456789012345678901234567890n;
console.log(largeNumber * 2n);
// 2469135780246913578024691357802469135780n
2. Cryptography:
function factorial(n) {
if (n === 0n) return 1n;
return n * factorial(n - 1n);
}
console.log(factorial(20n));
// 2432902008176640000n
3. Precise Calculations:
const microseconds = 1234567890123456n;
const seconds = microseconds / 1000000n;
console.log(seconds); // 1234567890n
Conclusion
BigInt is a valuable addition to JavaScript that enables working with large integers without loss of precision. It is particularly useful for applications requiring exact calculations with large numbers, such as cryptography, financial calculations, or working with timestamps in microseconds.
Use BigInt in your next project when you need to work with large integers!