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

📚 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:

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: false

2️⃣ 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: 18446744073709551616n

3️⃣ 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

🎯 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: false

Quick Reference

TaskCode
Literal10n
From number/stringBigInt(x)
Back to numberNumber(b)
Type checktypeof b === "bigint"
Serializeb.toString()

📚 Keep Learning

🎉 Lesson Complete — BigInt & Number Precision!

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

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.