JavaScript, when working with numbers, can sometimes struggle to accurately represent very large or very small values. However, with the introduction of the BigInt type in ECMAScript 2020, a solution to this issue is provided. BigInt is a data type in JavaScript used to represent very large integers. In this article, we will learn how to use BigInt in JavaScript and why it is important.
What is BigInt?
BigInt provides the largest integers that can be represented in JavaScript. It is used to represent numbers larger than 2^53-1 (Number.MAX_SAFE_INTEGER) or smaller than -2^53+1 (Number.MIN_SAFE_INTEGER). BigInt can be created by appending “n” to the end of a number or using the “BigInt()” function.
const largeNumber = 1234567890123456789012345678901234567890n;
const anotherLargeNumber = BigInt("9876543210987654321098765432109876543210");
Mathematical Operations with BigInt
With the BigInt type, you can perform standard arithmetic operations. Addition, subtraction, multiplication, and division operations are also valid for BigInt types.
const number1 = 1234567890123456789012345678901234567890n;
const number2 = 9876543210987654321098765432109876543210n;
const sum = number1 + number2;
const difference = number1 - number2;
const product = number1 * number2;
const quotient = number2 / number1;
console.log(sum); // Output: 11111111111111111110111111111111111111100n
console.log(difference); // Output: -864197531086419753208642975310864197530n
console.log(product); // Output: 12193263111263526934868788623952883390147903256800234567890123456790123456790n
console.log(quotient); // Output: 8n
Conversion Between BigInt and Standard Numbers
It is possible to convert between BigInt and standard numbers. To convert a BigInt to a standard number, we can use the Number() function. However, fractional parts are lost during this conversion.
const largeNumber = 1234567890123456789012345678901234567890n;
const standardNumber = Number(largeNumber);
console.log(standardNumber); // Output: 1.2345678901234568e+39
Modular Arithmetic with BigInt
BigInt can also be used in modular arithmetic operations. This feature, commonly used in cryptography and mathematics, is important when working with large numbers.
const largeNumber = 1234567890123456789012345678901234567890n;
const mod = 9876543210987654321098765432109876543210n;
const result = largeNumber % mod;
console.log(result); // Output: 2469135781357903782469135781357900000000n
Bitwise Operators with BigInt
Some operators used with Bitwise operators in standard numbers cannot be used with BigInt. However, Bitwise operators can be used with BigInt.
const largeNumber = 1234567890123456789012345678901234567890n;
const mask = 0b1111n;
const result = largeNumber & mask;
console.log(result); // Output: 10n
Conclusion
BigInt is a useful tool in JavaScript for representing and performing operations on very large integers. It provides an important solution for those who want accurate results when working with large numbers. However, when working with standard numbers, it is essential to be cautious about performance.