BigInt & Number Precision
Reviewed & published by Brayan K
BigInt is a numeric type for integers of arbitrary size — written with an n suffix like 10n — that stays exact beyond Number.MAX_SAFE_INTEGER, where ordinary floating-point numbers start to round.
Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
It is the right tool for huge IDs, precise counters, and big sums where losing a single digit would be a real bug.
What You'll Learn in This Lesson
- See where Number precision breaks
- Create BigInt with 10n and BigInt()
- Do exact big-integer arithmetic
- Know why you can't mix the types
- Understand integer-only division
- Handle the JSON.stringify caveat
📚 Prerequisites: You should know JavaScript numbers and basic arithmetic. BigInt is a second numeric type that sits alongside the regular number.
🔢 Real-World Analogy: A regular number is a calculator with a fixed screen width:
- • It shows small and medium numbers perfectly
- • Past a certain size the last digits get rounded off — they no longer fit
- • BigInt is a roll of paper tape: it just keeps adding digits, exactly, as far as you need
1️⃣ Where Number Precision Breaks Down
JavaScript numbers are 64-bit doubles. They store integers exactly only up to Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). Above that, consecutive integers start to collide — x + 1 can equal x. That's why a 19-digit database ID stored as a plain number can silently corrupt.
Check safety: Number.isSafeInteger(x) tells you whether an integer can be represented exactly as a regular number.
console.log(Number.MAX_SAFE_INTEGER); // ✅ Expected output: 9007199254740991
const max = 9007199254740991;
console.log(max + 1); // ✅ Expected output: 9007199254740992
console.log(max + 2); // ✅ Expected output: 9007199254740992
console.log(max + 1 === max + 2); // ✅ Expected output: true (both rounded!)
console.log(Number.isSafeInteger(max)); // ✅ Expected output: true
console.log(Number.isSafeInteger(max + 2)); // ✅ Expected output: false2️⃣ Creating BigInt & Exact Arithmetic
Make a BigInt by appending n to an integer literal, or by calling BigInt(value) on a number or numeric string. Arithmetic works as usual — +, -, *, ** — and stays exact no matter how large. Its typeof is "bigint".
Integer division truncates: there are no fractions in BigInt, so 7n / 2n is 3n, not 3.5. The remainder is available via %.
const big = 9007199254740991n;
console.log(big + 2n); // ✅ Expected output: 9007199254740993n
console.log(typeof 10n); // ✅ Expected output: bigint
// From a string for truly huge values
const huge = BigInt("123456789012345678901234567890");
console.log(huge * 2n); // ✅ Expected output: 246913578024691357802469135780n
// Integer-only division
console.log(7n / 2n); // ✅ Expected output: 3n
console.log(7n % 2n); // ✅ Expected output: 1n
console.log(2n ** 64n); // ✅ Expected output: 18446744073709551616n3️⃣ No Mixing, and the JSON Caveat
You cannot mix BigInt and Number in the same arithmetic — 10n + 5 throws a TypeError. Convert one side first: 10n + BigInt(5) or Number(10n) + 5. Loose equality (==) compares across types, but strict equality (===) also checks the type. And JSON.stringify throws on a BigInt unless you convert it (often to a string) yourself.
Use BigInt for IDs and counters where exactness matters. For money, prefer integer cents in a regular number, or a decimal library — BigInt has no fractions.
// Mixing throws — you must convert one side
try {
console.log(10n + 5);
} catch (e) {
console.log(e.constructor.name); // ✅ Expected output: TypeError
}
console.log(10n + BigInt(5)); // ✅ Expected output: 15n
console.log(Number(10n) + 5); // ✅ Expected output: 15
console.log(10n == 10); // ✅ Expected output: true
console.log(10n === 10); // ✅ Expected output: false
// Serialize a BigInt safely as a string
const order = { id: 9007199254740993n };
console.log(JSON.stringify({ id: order.id.toString() }));
// ✅ Expected output: {"id":"9007199254740993"}🎯 Your Turn — Make a BigInt
Add two BigInt values exactly. Remember both operands must be BigInt.
const max = 9007199254740991n;
// 👉 replace ___ : add 10 as a BigInt (use the n suffix)
const result = max + ___;
console.log(result);
// ✅ Expected output: 9007199254741001n🧠 Quick Recall
Predict the output before revealing the answer.
bigint — the n suffix makes a BigInt, a distinct primitive type.
2n — BigInt division truncates toward zero; there are no decimals.
false — strict equality also compares the type, and bigint !== number.
Common Errors & Fixes
- 1️⃣ Mixing BigInt and Number in arithmetic
- 2️⃣ Expecting decimals from BigInt division
- 3️⃣ Passing a decimal to BigInt()
- 4️⃣ JSON.stringify on a raw BigInt
🎯 Mini Challenge — Sum Huge Numbers
Add a list of very large integers exactly using BigInt and reduce.
const values = [9007199254740991n, 9007199254740991n, 9007199254740991n];
// 👉 sum them exactly; start the accumulator at 0n
const total = values.reduce((sum, v) => sum + v, 0n);
console.log(total);
// ✅ Expected output: 27021597764222973n
// A regular-number sum would lose precision here
console.log(Number.isSafeInteger(Number(total))); // ✅ Expected output: falseQuick Reference
| Task | Code |
|---|---|
| Literal | 10n |
| From number/string | BigInt(x) |
| Back to number | Number(b) |
| Type check | typeof b === "bigint" |
| Serialize | b.toString() |
📚 Keep Learning
- MDN: BigInt reference
- MDN: Number.MAX_SAFE_INTEGER
- MDN: Number.isSafeInteger()
🎉 Lesson Complete — BigInt & Number Precision!
- ✅ Regular numbers lose precision past MAX_SAFE_INTEGER
- ✅ Create BigInt with 10n or BigInt(x)
- ✅ Arithmetic stays exact at any size; division truncates
- ✅ You can't mix BigInt and Number — convert explicitly
- ✅ Serialize BigInt as a string for JSON
Practice quiz
What is the value of Number.MAX_SAFE_INTEGER?
- 9007199254740991
- 18446744073709551616
- 2147483647
- Infinity
Answer: 9007199254740991. Regular numbers store integers exactly only up to 9007199254740991 (about 9 quadrillion).
How do you write a BigInt literal for ten?
- 10b
- 10n
- BigInt10
- (10)
Answer: 10n. Appending n to an integer literal creates a BigInt, e.g. 10n.
What is typeof 5n?
- 'number'
- 'bigint'
- 'object'
- 'int'
Answer: 'bigint'. BigInt is a distinct primitive type, so typeof 5n is 'bigint'.
What does 7n / 2n evaluate to?
- 3.5
- 3n
- 4n
- 3
Answer: 3n. BigInt division truncates toward zero, so 7n / 2n is 3n.
What does 10n + 5 do?
- Returns 15n
- Returns 15
- Throws a TypeError
- Returns '105'
Answer: Throws a TypeError. You cannot mix BigInt and Number in arithmetic; it throws a TypeError. Convert one side first.
What is the result of 10n === 10?
- true
- false
- TypeError
- undefined
Answer: false. Strict equality also checks the type, and bigint is not number, so it is false (though 10n == 10 is true).
What does JSON.stringify({ id: 10n }) do?
- Returns {"id":10}
- Returns {"id":"10"}
- Throws a TypeError
- Returns {"id":10n}
Answer: Throws a TypeError. JSON.stringify throws a TypeError on a raw BigInt; convert it to a string first (e.g. 10n.toString()).
What does BigInt(1.5) do?
- Returns 1n
- Returns 2n
- Throws a RangeError
- Returns 1.5n
Answer: Throws a RangeError. BigInt() only accepts integers; a non-integer like 1.5 throws a RangeError.
What is 9007199254740991n + 2n?
- 9007199254740992n
- 9007199254740993n
- 9007199254740991n
- 9007199254740992
Answer: 9007199254740993n. BigInt stays exact beyond MAX_SAFE_INTEGER, so the result is 9007199254740993n.
What is the recommended use for BigInt?
- Money with cents
- Measurements with decimals
- Exact large integers like huge IDs and counters
- Replacing all numbers
Answer: Exact large integers like huge IDs and counters. BigInt is for exact large integers; for money with cents prefer integer cents or a decimal library, since BigInt has no fractions.
Continue this course
- Previous: Proxy & Reflect (Metaprogramming)
- Next: Checkpoint: Modern JavaScript — Combine modern syntax, methods and a Proxy in a build — then a quiz
- Quick reference: JavaScript cheat sheet
Frequently asked questions
Why does JavaScript lose precision with very large numbers?
Regular numbers are 64-bit floating point, which can only represent integers exactly up to Number.MAX_SAFE_INTEGER (about 9 quadrillion). Beyond that, some integers simply cannot be stored, so 9007199254740993 may round to 9007199254740992. BigInt was added to handle integers of arbitrary size exactly.
How do I create a BigInt?
Append n to an integer literal (10n) or call the BigInt() function (BigInt(10) or BigInt('9007199254740993')). Both produce a BigInt value whose typeof is 'bigint'. Use the n literal for constants and the function when converting a number or numeric string.
Can I mix BigInt and regular numbers in arithmetic?
No. 10n + 5 throws a TypeError because JavaScript will not silently mix the two types. You must convert explicitly: 10n + BigInt(5), or Number(10n) + 5. Comparisons with == do work across types (10n == 10 is true), but === checks the type too.
What are BigInt's limitations?
BigInt is integer-only — there are no decimals, and division truncates toward zero (7n / 2n is 3n). It cannot be used with the Math object, and JSON.stringify throws on a BigInt unless you provide a replacer. Use it for exact large integers like IDs and counters, not for money with cents or measurements.