JavaScript BigInt
The `BigInt` type in JavaScript is a built-in object that provides a way to represent and work with integers that are larger than the maximum value for the `Number` type. This is useful for applications requiring precise calculations with very large integers.
1. Creating BigInt Values
You can create `BigInt` values by appending an `n` to the end of an integer literal or by using the `BigInt` constructor function.
// Using BigInt literals
const bigInt1 = 1234567890123456789012345678901234567890n;
// Using BigInt constructor
const bigInt2 = BigInt("1234567890123456789012345678901234567890");
console.log(bigInt1); // 1234567890123456789012345678901234567890n
console.log(bigInt2); // 1234567890123456789012345678901234567890n
2. Operations with BigInt
`BigInt` supports basic arithmetic operations such as addition, subtraction, multiplication, and division. Note that you cannot mix `BigInt` and `Number` values in operations directly; they must be explicitly converted.
const bigInt1 = 1234567890123456789012345678901234567890n;
const bigInt2 = 987654321098765432109876543210987654321n;
// Addition
const sum = bigInt1 + bigInt2;
console.log(sum); // 2222222222222222222222222222222222222221n
// Subtraction
const difference = bigInt1 - bigInt2;
console.log(difference); // 246913578024691357802469135780246913569n
// Multiplication
const product = bigInt1 * bigInt2;
console.log(product); // 121932631112635269454399593391008934796859490831644650252018232213540n
// Division
const quotient = bigInt1 / bigInt2;
console.log(quotient); // 1n
3. Converting Between BigInt and Number
Since `BigInt` and `Number` types are not directly interoperable, you need to convert between them when necessary. Use the `Number()` function to convert a `BigInt` to a `Number`, but be cautious as this may lead to precision loss. Conversely, use the `BigInt()` function to convert numbers to `BigInt`.
const bigInt = 1234567890123456789012345678901234567890n;
// Converting BigInt to Number (with caution)
const num = Number(bigInt);
console.log(num); // Warning: may lose precision
// Converting Number to BigInt
const smallNumber = 123456789;
const bigIntFromNumber = BigInt(smallNumber);
console.log(bigIntFromNumber); // 123456789n
4. BigInt Methods
`BigInt` objects have a few methods, but they are limited compared to `Number`. You can use methods like `.toString()` to get the string representation and `.valueOf()` to get the primitive value.
const bigInt = 1234567890123456789012345678901234567890n;
// Get the string representation
console.log(bigInt.toString()); // '1234567890123456789012345678901234567890'
// Get the primitive value (same as the original BigInt)
console.log(bigInt.valueOf()); // 1234567890123456789012345678901234567890n
5. Limitations and Considerations
While `BigInt` is useful for very large integers, be aware of the following limitations:
- Cannot be used with `Math` methods (e.g., `Math.sqrt()`).
- Cannot be mixed with `Number` values in expressions without explicit conversion.
- Not supported in all environments (e.g., older JavaScript engines).