JavaScript BigInt. Working with Large Numbers
A detailed guide on the BigInt type in JavaScript, covering its characteristics, usage, and practical examples
Last updated: 2024-12-18Today, we're going to talk about one of JavaScript's amazing features - BigInt. Introduced with ES2020 (ES11), BigInt allows us to work with extremely large numbers. Let's start our journey into the world of BigInt!
What is BigInt?
BigInt is a primitive data type in JavaScript used to represent very large integers. It allows us to work with numbers larger than 2^53 - 1, which is the limit for the standard Number type.
Let's look at the difference between a regular Number and a BigInt:
const bigNumber = 9007199254740991n; // BigInt
const regularBigNumber = 9007199254740991; // Number
console.log(bigNumber === regularBigNumber); // false
console.log(typeof bigNumber); // "bigint"
console.log(typeof regularBigNumber); // "number"
In this example, bigNumber
is a BigInt type, while regularBigNumber
is a regular Number type. Although their values look the same, they are different data types.
Ways to Create BigInt
There are two main ways to create a BigInt value:
- Append 'n' to the end of an integer:
const bigInt1 = 1234567890123456789012345678901234567890n;
- Use the BigInt() function:
const bigInt2 = BigInt("1234567890123456789012345678901234567890");
const bigInt3 = BigInt(1234567890); // For smaller numbers
Arithmetic Operations with BigInt
You can perform all basic arithmetic operations with BigInts:
const a = 1234567890123456789n;
const b = 9876543210987654321n;
console.log(a + b); // 11111111111111111110n
console.log(a - b); // -8641975320864197532n
console.log(a * b); // 12193263111263526900986223731469970n
console.log(a / b); // 0n (BigInt only returns integer results)
console.log(a % b); // 1234567890123456789n
console.log(a ** 2n); // 1524157875323883675049535156249n
Note that BigInts only return integer division results.
Limitations of BigInt
BigInts are very useful, but they have some limitations:
- You can't mix BigInts with Numbers in operations:
const bigInt = 10n;
const number = 5;
// console.log(bigInt + number); // TypeError: Cannot mix BigInt and other types
console.log(bigInt + BigInt(number)); // 15n
- BigInts don't work with Math object methods:
// console.log(Math.max(5n, 10n)); // TypeError: Cannot convert a BigInt value to a number
console.log(5n > 10n ? 5n : 10n); // 10n (comparison works)
- BigInts lose fractional parts:
console.log(5n / 2n); // 2n (not 3n)
Comparing BigInts
You can compare BigInts with each other and with Numbers:
console.log(5n === 5); // false (types are different)
console.log(5n == 5); // true (values are equal)
console.log(10n > 5); // true
console.log(5n < 10); // true
console.log(5n <= 5); // true
console.log(5n >= 5); // true
Converting BigInts to Strings
You can convert BigInts to strings using the toString()
method or simple string conversion:
const bigNumber = 1234567890123456789012345678901234567890n;
console.log(bigNumber.toString()); // "1234567890123456789012345678901234567890"
console.log(String(bigNumber)); // "1234567890123456789012345678901234567890"
console.log(bigNumber + ""); // "1234567890123456789012345678901234567890"
Practical Applications of BigInts
BigInts can be very useful in the following scenarios:
- Cryptography and security:
function checkLargePrime(num) {
if (num <= 1n) return false;
for (let i = 2n; i * i <= num; i++) {
if (num % i === 0n) return false;
}
return true;
}
const largeNumber = 2n ** 256n - 1n; // Mersenne prime
console.log(checkLargePrime(largeNumber)); // true
- Working with time (milliseconds):
const startTime = BigInt(Date.now());
// Long-running operation
const endTime = BigInt(Date.now());
const difference = endTime - startTime;
console.log(`Operation took ${difference} milliseconds`);
- Large numerical calculations:
function factorial(n) {
if (n === 0n) return 1n;
return n * factorial(n - 1n);
}
console.log(factorial(50n)); // 30414093201713378043612608166064768844377641568960512000000000000n
BigInts and JSON
BigInts can't be directly converted to JSON. A special approach is needed:
const data = { bigNumber: 1234567890123456789012345678901234567890n };
const json = JSON.stringify(data, (key, value) =>
typeof value === 'bigint' ? value.toString() : value
);
console.log(json); // {"bigNumber":"1234567890123456789012345678901234567890"}
const parsedData = JSON.parse(json, (key, value) =>
key === 'bigNumber' ? BigInt(value) : value
);
console.log(parsedData.bigNumber); // 1234567890123456789012345678901234567890n
Conclusion
BigInt is an amazing feature in JavaScript that provides the ability to work with large numbers. It's very useful in cryptography, precise calculations, and working with big data. By understanding the unique properties and limitations of BigInts, you can use them correctly and efficiently.
Frequently Asked Questions (FAQ)
- Q: When should BigInt be used? A: BigInt should be used when working with integers larger than 2^53 - 1 or smaller than -2^53 + 1. This is primarily applicable in cryptography, large numerical calculations, and other fields where precision is crucial.
- Q: Can BigInts be converted to Numbers and vice versa? A: Yes, but with caution:
const bigInt = 123456789012345678901234567890n;
const number = Number(bigInt); // Precision may be lost
const backToBigInt = BigInt(Math.floor(number)); // Only the integer part is taken
- Q: How do BigInts affect performance? A: BigInts generally perform slower than Numbers because they require more memory and perform more complex operations. They are more efficient for large numbers, but for small numbers, regular Numbers are preferable.
- Q: Are BigInts supported in all browsers? A: BigInts are supported in modern browsers, but there might be issues with older versions. It's recommended to check browser compatibility or use a polyfill before using them.
- Q: How can fractional numbers be represented with BigInts? A: BigInts only represent integers. To work with fractional numbers, you can use a scaling approach:
const fraction = 1234567890123456789n * 100n / 3n; // 411522630041152263n
console.log(fraction / 100n); // 4115226300411522n (approximately 41152263004115.22)
- Q: Do BigInts consume a lot of memory? A: Yes, BigInts generally consume more memory than regular Numbers. They allocate memory dynamically, so very large numbers can consume significant amounts of memory.
- Q: What security considerations should be taken into account when working with BigInts? A: BigInts are important from a security perspective, especially in cryptographic operations. It's crucial to use constant-time operations to protect against timing attacks. Also, be aware that converting BigInts to regular numbers can lead to precision loss.
These questions and answers clarify common queries about BigInts and highlight important points to consider when using them.