What is BigInt ?
JavaScript BigInt is a special data type introduced in ES2020 to handle large integers beyond the limit of the Number type. Unlike regular numbers, BigInt allows you to perform calculations with values larger than 253−12^{53} – 1253−1 (the maximum safe integer for the Number type).
Why Use BigInt?
JavaScript’s Number type is limited to a safe integer range of −253+1-2^{53} + 1−253+1 to 253−12^{53} – 1253−1. Calculations beyond this range may result in inaccuracies. BigInt ensures precise arithmetic for values outside this range, making it essential for scenarios like cryptography, astronomical calculations, or handling IDs.
Creating BigInt
You can create a BigInt in two ways:
- Appending
n
to a number - Using the BigInt() constructor
Example: Creating BigInt
// Using 'n' suffix
let bigInt1 = 123456789012345678901234567890n;
// Using BigInt constructor
let bigInt2 = BigInt("123456789012345678901234567890");
console.log(bigInt1); // Output: 123456789012345678901234567890n
console.log(bigInt2); // Output: 123456789012345678901234567890n
BigInt Characteristics
BigInt Values Are Integers Only
BigInt does not support decimals. Trying to create a BigInt with a decimal value throws an error.
let invalidBigInt = 10.5n; // SyntaxError: Cannot use decimals
BigInt and Number Are Separate Types
BigInt is distinct from the Number type. Operations between BigInt and Number require explicit conversion.
Operations with BigInt
BigInt supports basic arithmetic operations like addition, subtraction, multiplication, division and modulus.
Example: Arithmetic Operations
let a = 9007199254740991n; // Maximum safe integer in Number
let b = 12345678901234567890n;
// Addition
console.log(a + b); // Output: 12345678910241757881n
// Multiplication
console.log(a * 2n); // Output: 18014398509481982n
// Division
console.log(b / 3n); // Output: 4115226300411522630n (rounded down)
Comparison Between BigInt and Number
You can compare BigInt and Number directly, but ensure type consistency for accurate results.
Example: Comparison
let num = 42;
let bigInt = 42n;
console.log(num == bigInt); // Output: true (type conversion occurs)
console.log(num === bigInt); // Output: false (strict comparison)
BigInt Methods
BigInt does not have as many built-in methods as Number, but it supports certain global functions:
BigInt.asIntN(width, value)
Converts a BigInt to a signed integer with a specific width.
let value = BigInt("255");
console.log(BigInt.asIntN(8, value)); // Output: -1 (fits in 8-bit signed integer)
BigInt.asUintN(width, value)
Converts a BigInt to an unsigned integer with a specific width.
console.log(BigInt.asUintN(8, value)); // Output: 255 (fits in 8-bit unsigned integer)
Converting Between BigInt and Number
Since BigInt and Number are separate types, explicit conversion is necessary for calculations involving both types.
Example: Conversion
let bigInt = 100n;
let num = 50;
// Convert Number to BigInt
let result = bigInt + BigInt(num);
console.log(result); // Output: 150n
// Convert BigInt to Number
let resultNum = Number(bigInt) + num;
console.log(resultNum); // Output: 150
Limitations of BigInt
- No Decimals: BigInt supports only integers.
- Incompatibility with Math Methods: BigInt cannot be used with the Math object.
Example: Unsupported Math Methods
let bigInt = 100n;
console.log(Math.sqrt(bigInt)); // TypeError: Cannot convert BigInt to number
- Performance: BigInt operations may be slower than Number for smaller values.
Practical Use Cases
- Cryptography: BigInt is essential for large prime number calculations used in encryption algorithms.
- Handling Large Identifiers: BigInt can handle large user IDs or record numbers.
- Accurate Financial Calculations: BigInt avoids precision errors common with floating-point arithmetic.
Example: Handling Large IDs
let userID = BigInt("98765432101234567890");
console.log(`User ID: ${userID}`);
// Output: User ID: 98765432101234567890n