JavaScript BigInt
JavaScript's BigInt
is a special numeric type that allows you to work with integers beyond the safe range of the Number
type. It provides a way to represent whole numbers larger than Number.MAX_SAFE_INTEGER
(253 - 1) without losing precision.
Why Use BigInt?
In JavaScript, the Number
type is represented using a double-precision 64-bit binary format (IEEE 754). This means that while it can handle very large numbers, there's a limit to the precision it can maintain. The largest integer that can be safely represented by a Number
is Number.MAX_SAFE_INTEGER
(9,007,199,254,740,991).
For numbers larger than this, JavaScript introduces BigInt
to allow accurate representation of large integers without losing precision.
Creating a BigInt
There are two ways to create a BigInt
:
- By appending
n
to the end of an integer literal. - By calling the
BigInt()
function.
// Creating BigInt with 'n' suffix
const bigInt1 = 1234567890123456789012345678901234567890n;
console.log(bigInt1); // Output: 1234567890123456789012345678901234567890n
// Creating BigInt with BigInt() function
const bigInt2 = BigInt("1234567890123456789012345678901234567890");
console.log(bigInt2); // Output: 1234567890123456789012345678901234567890n
BigInt Arithmetic
You can perform arithmetic operations with BigInt
just like you would with regular numbers. However, you cannot mix BigInt
with Number
types directly; they must be of the same type or converted first.
// Addition
const sum = 10000000000000000000000000n + 20000000000000000000000000n;
console.log(sum); // Output: 30000000000000000000000000n
// Subtraction
const difference = 30000000000000000000000000n - 15000000000000000000000000n;
console.log(difference); // Output: 15000000000000000000000000n
// Multiplication
const product = 2000000000n * 3000000000n;
console.log(product); // Output: 6000000000000000000n
// Division
const quotient = 20000000000000000000n / 3000000000n;
console.log(quotient); // Output: 6666666666n
Notice that the division of two BigInt
values results in another BigInt
, and the result is rounded towards zero, removing any fractional part.
Comparing BigInt and Number
While BigInt
can handle much larger integers, it operates differently from Number
. For example, mixing BigInt
and Number
types directly in operations will result in an error.
const bigIntValue = 12345678901234567890n;
const numberValue = 20;
// This will throw an error
// console.log(bigIntValue + numberValue);
// You need to explicitly convert one type to the other
console.log(bigIntValue + BigInt(numberValue)); // Convert number to BigInt
console.log(Number(bigIntValue) + numberValue); // Convert BigInt to number (may lose precision)
Limitations of BigInt
BigInt
has some limitations:
- It cannot be used with built-in JavaScript methods that expect
Number
values (e.g.,Math
functions). - It does not support decimal places, making it unsuitable for precise floating-point calculations.
Conclusion
JavaScript's BigInt
is a powerful tool for working with very large integers that exceed the safe range of the Number
type. While it has some limitations, it provides an essential capability for applications requiring precise handling of large numeric values.
Comments
Post a Comment