What is BigInt in JavaScript and when would you use it?
BigInt is a primitive for whole numbers of any size, written with an n suffix like 123n. Use it when values pass Number.MAX_SAFE_INTEGER (2^53 - 1), such as large IDs. It cannot be mixed with regular numbers.
Explain it simply
A normal JavaScript number is a 64-bit float. It can only represent whole numbers exactly up to 9,007,199,254,740,991. Past that, neighbouring integers start to collapse into the same value, like a cheap calculator that rounds big totals.
BigInt removes the ceiling. Add n to a literal or call BigInt("..."). Arithmetic works, but you cannot mix BigInt and Number in one expression: JavaScript throws a TypeError instead of guessing.
Division drops the fraction (7n / 2n is 3n), and JSON.stringify cannot serialise a BigInt without help.
Example
console.log(Number.MAX_SAFE_INTEGER); // Output: 9007199254740991
console.log(9007199254740992 === 9007199254740993); // Output: true
console.log(9007199254740992n === 9007199254740993n); // Output: false
console.log(typeof 10n); // Output: bigint
console.log(String(7n / 2n)); // Output: 3
console.log(10n > 5); // Output: true
console.log(10n == 10); // Output: true
console.log(10n === 10); // Output: false10n + 5 would throw a TypeError.
What to say in the interview
"BigInt is the seventh primitive, for integers beyond 2^53 - 1 where Number loses precision, for example 64-bit database IDs or exact factorials. It cannot be mixed with Number in arithmetic, division truncates, and it is not JSON-serialisable by default."
Common mistakes
- Mixing 5n + 1 and expecting a result. It throws.
- Using BigInt for decimals. It only holds integers.