BigInt is a special numeric type in JavaScript that allows you to represent integers with arbitrary precision. It was introduced in ECMAScript 2020 to handle numbers larger than the Number.MAX_SAFE_INTEGER
(2^53 – 1) and smaller than Number.MIN_SAFE_INTEGER
(-(2^53 – 1)).
1. Creating BigInt
You can create a BigInt by appending an n
to the end of an integer literal or by using the BigInt()
function.
// Using the 'n' suffix let bigIntNum = 1234567890123456789012345678901234567890n; // Using the BigInt() function let bigIntNum2 = BigInt('1234567890123456789012345678901234567890');
2. BigInt vs Number
BigInt is designed for precise integer arithmetic, whereas Number
is a double-precision floating-point number.
let bigInt = 9007199254740991n; // BigInt let number = 9007199254740991; // Number console.log(bigInt + 1n); // Output: 9007199254740992n console.log(number + 1); // Output: 9007199254740992 (but can't go beyond safely)
Key Differences:
- BigInt can handle very large integers without losing precision.
- Number has a safe integer limit (
Number.MAX_SAFE_INTEGER
).
3. BigInt Arithmetic
You can perform standard arithmetic operations with BigInt, similar to regular numbers. However, both operands must be BigInts.
let a = 1000000000000000000n; let b = 2000000000000000000n; console.log(a + b); // Output: 3000000000000000000n console.log(a - b); // Output: -1000000000000000000n console.log(a * b); // Output: 2000000000000000000000000000000000000n console.log(b / a); // Output: 2n console.log(b % a); // Output: 0n
Note: Division with BigInt returns a whole number.
4. Mixing BigInt and Number
Mixing BigInt and Number in an operation will result in a TypeError. You need to convert the Number to a BigInt or vice versa.
let bigInt = 100n; let number = 50; // Convert Number to BigInt console.log(bigInt + BigInt(number)); // Output: 150n // Convert BigInt to Number (use cautiously for very large values) console.log(Number(bigInt) + number); // Output: 150
5. Comparisons
You can compare BigInt with other BigInt values or numbers using comparison operators.
let bigInt = 100n; let number = 100; console.log(bigInt > number); // Output: false console.log(bigInt === BigInt(number)); // Output: true
Note: Strict equality (===
) will return false
if comparing a BigInt and a Number, even if their values are the same.
6. BigInt Methods
toString()
: Converts a BigInt to a string.let bigInt = 12345678901234567890n; console.log(bigInt.toString()); // Output: "12345678901234567890"
toLocaleString()
: Converts a BigInt to a locale-specific string.let bigInt = 12345678901234567890n; console.log(bigInt.toLocaleString()); // Output: "12,345,678,901,234,567,890"
7. Use Cases for BigInt
- Cryptography: Handling large numbers for encryption.
- Scientific Calculations: Performing calculations that require high precision.
- Financial Applications: Managing large financial transactions without losing precision.
Summary of JavaScript BigInt
- BigInt is used for working with very large integers.
- Arbitrary Precision: BigInt can represent integers with more precision than
Number
. - Arithmetic Operations: Supports addition, subtraction, multiplication, division, and modulo.
- No Mixing: Cannot mix BigInt and Number directly in operations.
BigInt provides a reliable way to handle large integers in JavaScript, ensuring precision and correctness in calculations that go beyond the capabilities of the standard Number
type.