JavaScript : BigInt

In JavaScript, BigInt is a relatively new data type introduced as part of the ES2020 (also known as ES11) specification. It allows you to represent integers of arbitrary length, overcoming the limitations of the standard Number type, which is constrained by the IEEE 754 double-precision floating-point format.

Here's an overview of BigInt and how to use it:

1. BigInt Literal:


You can create a BigInt by appending an n to the end of an integer literal or by using the BigInt() constructor.


const bigIntLiteral = 1234567890123456789012345678901234567890n;
const fromConstructor = BigInt("1234567890123456789012345678901234567890");

 

2. Operations with BigInt:


You can perform arithmetic operations with BigInts just like regular numbers, using standard operators such as + , - , * , / , and .


const a = 123n;
const b = 456n;
const sum = a + b;        // 579n
const product = a * b;    // 56088n

 

3. Mixing BigInt with Number:


You can mix BigInts with regular numbers (Numbers), but the result will always be a BigInt.


const c = 123;
const sumWithNumber = a + c;  // 246n

 

4. Comparison with Number:


You can compare BigInts with regular numbers using standard comparison operators ( > , < , >= , <= , ==  , != ). When comparing a BigInt with a regular number, the BigInt is automatically converted to a number for comparison.


const a = 123n;
const b = 456;
console.log(a > b);  // false
console.log(a < b);  // true
console.log(a == b); // false (even though values are equal, types are different)

 

5. BigInt Methods:


BigInts have their own set of methods, including toString() to convert to a string, valueOf() to convert to a primitive number, and toLocaleString() for localized string representations.


const bigInt = 12345678901234567890n;
console.log(bigInt.toString());       // "12345678901234567890"
console.log(bigInt.valueOf());        // 12345678901234567890
console.log(bigInt.toLocaleString()); // "12,345,678,901,234,567,890"

 

6. Limitations:


Although BigInts can represent arbitrarily large integers, they cannot be used interchangeably with regular numbers (Numbers) in all contexts. For example, you cannot use BigInts with bitwise operators ( & , | ,  << , >> , etc.) or in places where only primitive values are allowed, such as object keys or array indices.

Example Usage:

Here's an example demonstrating the use of BigInts in JavaScript:


const bigInt1 = 12345678901234567890n;
const bigInt2 = BigInt("987654321098765432109876543210");

const sum = bigInt1 + bigInt2;
console.log(sum);  // 12345678902222222222n

 

Conclusion:


BigInts provide a way to represent integers of arbitrary length in JavaScript, overcoming the limitations of the standard Number type. They are particularly useful for scenarios where precise integer arithmetic is required, such as cryptography, financial calculations, or working with very large numbers. However, due to their nature as a separate data type, BigInts cannot be used interchangeably with regular numbers in all contexts, and you need to be mindful of this when working with them in your code.