BigInt in JavaScript
Sometimes, you just need big numbers. 9,007,199,254,740,991 is a number that is too low! Well, in JavaScript, there is the BigInt type. This is a type that can represent numbers above 2⁵³-1.
BigInts are created by adding a “n” to the end of the number. 2 ways to construction BigInts are “const bigInt = 9007199254740992n”, and “const biggerInt = BigInt(“9007199254740993”)”. You can also create a BigInt with a number that is much smaller that 2⁵³. “const smallBigInt = BigInt(17)” is the small as “17n”
Operators look the same as if they were numbers, “1n + 1n == 2n”, “14n — 4n == 10n”, “5n / 2n == 2”. These 3 operators work as stated above, but can we mix BigInts and Numbers? “14n + 3 == error”. The answer is no, we cannot mix the 2 types.
However, we can use comparisons normally. “5n > 1n == true” and “5 > 1n == true”. However, strictly will not see these as true “1 == 1n (True)”, “1 === 1n (False)”.