22. The BigInt Type
The standard Number type in JavaScript uses a 64-bit floating-point format. This means it can safely represent integers only up to 2^53 - 1 (or Number.MAX_SAFE_INTEGER). For numbers larger than this (common in cryptography, timestamps, or large databases), precision is lost.
BigInt, introduced more recently, allows us to work with arbitrarily large integers.
Creating a BigInt
You create a BigInt by appending the letter n to the end of an integer literal.
javascript const massiveNumber = 9007199254740991n; // Safe maximum in Number is 9007199254740991 const biggerThanSafe = massiveNumber + 1n;
console.log(biggerThanSafe); // Output: 9007199254740992n
// console.log(Number.MAX_SAFE_INTEGER + 1); // This calculation would lose precision
BigInt Limitations
BigInts cannot be mixed with standard Numbers in mathematical operations. You must convert them explicitly if needed.
javascript // console.log(10n + 5); // ERROR console.log(10n + BigInt(5)); // OK: 15n