Learn how to use BigInt in JavaScript to store large numbers

BigInt is a built-in object that provides a way to store numbers larger than Number.MAX_SAFE_INTEGER.
The reason for the need of BigInt is
var maxIntNum = 9007199254740991;
var largeNumber = maxIntNum + 2; // 9007199254740992
The expected result for the above operation is 9007199254740993, but we get 9007199254740992. The reason is that JavaScript uses 64 bits to store a number, so once we try to create a number larger than this, it can’t be stored.
To solve this problem, we can go for BigInt.
Creating BigInt
- Adding
nto end of a number
var num = 100000000000000000n;
num + 10n; // 100000000000000010n
// using binary literal with n appended
var num = 0x1fffffffffffffn
num; // 9007199254740991n
2. Using the BigInt constructor
var num = BigInt(100000000000000000);
num+10n; //100000000000000010n
var fromHex = BigInt('0x1fffffffffffff');
fromHex; // 9007199254740991n
Converting a BigInt to an object
var bigIntAsObject = Object(1n);
bigIntAsObject; // BigInt {1n}
Check if a number is a BigInt
typeof num === "bigint";
typeof bigIntAsObject; "object"
Remember, we cannot mix normal numbers with BitInt, and if we try to, it results in an error.
num + 10;
// Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
To solve this, we can do the following by converting our normal number to a BigInt:
num + BigInt(10);
Arithmetic operations with BigInt
var num = BigInt(10);
Addition
num + 10n; // 20n
Subtraction
num - BigInt(5); // 15n
Multiplication
num * 10n; // 150n
Exponent operator
num ** 10n; // 10000000000n
DIVISION
// when we perform division , the fractional part in the result is removed
var n = 5n;
n / 2n; // 2n
MODULO
var n = 5n;
n % 2n; // 1n
Equality Comparison
10n == 10; // true
When BigInt is compared with non-BigInt numbers, using strict-equality === always results in false.
10n === 10; // false
Relational Operations
It is similar to number comparisons
1n < 2
// true
2n > 1
// true
2 > 2
// false
2n > 2
// false
2n >= 2
// true
Converting to Boolean
Boolean(0n) // false
Boolean(12n) // true
!0n; // true
!12n; // false.
Parsing with JSON
var n = 100n;
JSON.stringify(n);
// Uncaught TypeError: Do not know how to serialize a BigInt
Methods
toString
The toString method of BigInt will return a string representing the specified BigInt object without n at the end of the number.
var a = 10n;
a.toString(); //10
We can use the toString() method to solve errors caused by the stringify method. To solve this, we will add the toJSON method to the BigInt prototype.
BigInt.prototype.toJSON = function() {
return this.toString();
}
valueOf
The valueOf() method returns the wrapped primitive value of a BigInt object.
var obj = Object(10n);
obj.valueOf(); // 10n
Reference: MDN
Follow Javascript Jeep🚙💨.
https://sitepoint.tapfiliate.com/p/payout-methods/new/
https://sitepoint.tapfiliate.com/p/payout-methods/new/
https://sitepoint.tapfiliate.com/p/payout-methods/new/
https://sitepoint.tapfiliate.com/p/payout-methods/new/