BigInt
Introduction
BigInt is JavaScript's primitive type for arbitrary-precision integers. It solves the integer precision limit of number, which is safe only up to Number.MAX_SAFE_INTEGER (2^53 - 1).
Create BigInts with the n suffix (9007199254740993n) or the BigInt() function. BigInts cannot be mixed with Numbers in arithmetic, and Math methods do not accept them.
Why This Matters
BigInt appears whenever identifiers, counters, timestamps, cryptography, or financial integer units can exceed 53 safe bits. Interviewers ask it to see whether you know the number limit and the TypeError caused by mixing 1n + 1.
Theory
Why BigInt exists
JavaScript number is an IEEE-754 double. It can represent very large magnitudes, but it can only represent integers exactly up to Number.MAX_SAFE_INTEGER, which is 9007199254740991 (2^53 - 1). After that, adjacent integers may collapse to the same number.
| Topic | BigInt rule | Example |
|---|---|---|
| Literal | Add n suffix | 123n. |
| Constructor | Use BigInt(value) for integer strings/numbers | BigInt('123'). |
| Arithmetic | Use BigInt with BigInt | 1n + 2n. |
| Mixing | Number + BigInt throws | 1n + 1 is TypeError. |
| Division | Integer division truncates | 5n / 2n is 2n. |
| Math | Math.max(1n, 2n) throws | Convert carefully if safe. |
| JSON | JSON.stringify(1n) throws | Convert to string first. |
BigInt vs Number
Use number for normal arithmetic, decimals, UI measurements, and Math APIs. Use bigint for integers that must remain exact beyond the safe-number range. BigInt has no fractional values, so 1.5n is invalid.
Conversions
Convert intentionally. Number(big) may lose precision if the BigInt is outside the safe integer range. BigInt(number) requires the number to be an integer. For API payloads, BigInts are commonly serialized as strings.
Comparisons
Relational comparisons like 1n < 2 are allowed, but arithmetic mixing is not. Strict equality keeps types distinct: 1n === 1 is false, while loose equality 1n == 1 is true. Prefer strict equality and explicit conversion.
Visual Diagrams
Number safe integers
-(2^53 - 1) ---------------- 0 ---------------- (2^53 - 1)
9007199254740991
Beyond this boundary, Number integer precision is not guaranteed.
BigInt can keep growing exactly for integers.BigInt is for exact integers beyond the safe range of Number.
Code Examples
Safe integer collapse
Past Number.MAX_SAFE_INTEGER, two different mathematical integers can compare equal as Numbers.
BigInt arithmetic rules
Keep both operands BigInt, and remember that division truncates because BigInt represents integers only.
Playground
Press Run to execute the code and see output here.
Output Prediction
Predict the output #1
1console.log(Number.MAX_SAFE_INTEGER);2console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2);3console.log(String(9007199254740993n + 2n));4 5try {6 console.log(1n + 1);7} catch (error) {8 console.log(error.name);9}Interview Questions
1Why do we need BigInt if JavaScript already has Number?
number is a floating-point type and can only represent integers exactly up to Number.MAX_SAFE_INTEGER (2^53 - 1). BigInt represents integers with arbitrary precision, so it is used when exact large integers matter. It cannot represent fractions, cannot be used with Math methods, and cannot be mixed with Number in arithmetic without explicit conversion.
Follow-ups
- What happens with `1n + 1`?
- Can BigInt be serialized directly with JSON?
Quiz
1. What happens when JavaScript evaluates `1n + 1`?
2. Which value is the largest safe integer for JavaScript Number?
Summary
- BigInt represents arbitrary-precision integers with the `bigint` primitive type.
- Number integers are only safe up to `Number.MAX_SAFE_INTEGER` (`2^53 - 1`).
- Create BigInts with the `n` suffix or `BigInt()` for integer inputs.
- BigInt cannot be mixed with Number in arithmetic, cannot use `Math`, and should be serialized intentionally.
Cheat Sheet
Create: 123n, BigInt('123').
Use for: exact large integers beyond Number.MAX_SAFE_INTEGER.
No mixing: 1n + 1 throws TypeError; convert explicitly.
No decimals: BigInt is integer-only; division truncates.
No Math / JSON direct: Math.max(1n, 2n) throws; JSON.stringify(1n) throws — convert to string.