xxxxxxxxxx
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111") // 9007199254740991n
xxxxxxxxxx
const theBiggestInt = 9007199254740991n
const alsoHuge = BigInt(9007199254740991) // 9007199254740991n
const hugeString = BigInt("9007199254740991") // 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff") // 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111") // 9007199254740991n
xxxxxxxxxx
// Using BigInt for big integer operations
const bigNum1 = BigInt("12345678901234567890");
const bigNum2 = BigInt("98765432109876543210");
// Addition
const sum = bigNum1 + bigNum2;
console.log(sum); // Output: 111111111011111111100
// Subtraction
const diff = bigNum2 - bigNum1;
console.log(diff); // Output: 86419753208641975320
// Multiplication
const product = bigNum1 * bigNum2;
console.log(product); // Output: 121932631137021795424737112635269100
// Division
const quotient = bigNum2 / bigNum1;
console.log(quotient); // Output: 8000000000n
xxxxxxxxxx
Why BigInt ? - To represent integer values larger than 2^53
xxxxxxxxxx
let bigInt = 1n; // Using language construct
let bigInt2 = BigInt(1); // Using function
// Note: You cannot write new BigInt() because it is not a constructor
xxxxxxxxxx
// BigInt value
const value1 = 900719925124740998n;
// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"
const value2 = 900719925124740998n;
// Error! BitInt and number cannot be added
const result2 = value2 + 1;
console.log(result2);
xxxxxxxxxx
Why BigInt ? - To represent integer values larger than 2^53
xxxxxxxxxx
// Creating a bigint via the BigInt function
const oneHundred: bigint = BigInt(100);
// Creating a BigInt via the literal syntax
const anotherHundred: bigint = 100n;
Try
xxxxxxxxxx
// BigInt value
const value = 900719925124740998n;
// Adding two big integers
const value1 = value + 1n;
console.log(value1); // returns "900719925124740999n"