BigInt & Number Precision

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.

Learn BigInt & Number Precision in our free JavaScript course — an interactive lesson with runnable examples, a practice exercise and a quick reference.

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.

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

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.

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 % .

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.

Add two BigInt values exactly. Remember both operands must be BigInt.

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.

Add a list of very large integers exactly using BigInt and reduce.

Up next: Checkpoint: Modern JavaScript — put it all together. 🏁

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.