Back to JavaScript Notes
Topic #303

BigInt

You can represent and compute with whole numbers larger than Number.MAX_SAFE_INTEGER using BigInt literals and the BigInt() function.

What it is

BigInt is a primitive type for arbitrary-precision integers. JavaScript Number stores values as IEEE 754 doubles, so the largest integer that can be represented safely is 2^53 - 1, which is 9007199254740991. Beyond that limit, integers may silently lose precision. BigInt can represent whole numbers of any size, limited only by available memory. Related terms include Number.MAX_SAFE_INTEGER, Number.isSafeInteger(), the n literal suffix, the BigInt() constructor, and mixed BigInt/Number operations.

Why it matters

  • Exact IDs from databases or APIs that exceed the safe integer range.
  • Financial or scientific calculations that require exact integer arithmetic.
  • Bitwise operations on very large integers.
  • Counting or indexing huge collections without silent rounding.
  • Interoperability with APIs that expose 64-bit integer values.

Syntax or steps

  1. Create a BigInt by appending n to an integer literal.
  2. Create a BigInt from a string or Number with BigInt(value).
  3. Use normal arithmetic operators such as +, -, *, /, %, and ** with BigInt operands.
  4. Convert explicitly when mixing BigInt and Number.
const big = 9007199254740993n;
const fromString = BigInt("9007199254740993");
const sum = big + 1n;

Example

const safeLimit = Number.MAX_SAFE_INTEGER; // 9007199254740991
const tooBig = 9007199254740993n;

console.log(tooBig + 1n);          // 9007199254740994n
console.log(tooBig * 2n);          // 18014398509481986n
console.log(tooBig / 2n);          // 4503599627370496n
console.log(tooBig % 2n);          // 1n
console.log(tooBig > safeLimit);   // true
console.log(Number(tooBig));       // 9007199254740992 (precision lost)

In this example, 9007199254740993n is a BigInt literal. The operators +, *, /, and % produce BigInt results when both operands are BigInt. Division truncates toward zero, so tooBig / 2n drops the fractional part. Comparing a BigInt with a Number is allowed, but converting a large BigInt to Number can lose precision.

Common mistakes

  • Mixing BigInt and Number in arithmetic: 1n + 1 throws a TypeError. Fix it by converting with BigInt(1) or Number(1n) only when safe.
  • Using BigInt for decimals: BigInt(1.5) throws a RangeError. Fix it by scaling values, such as storing cents as BigInt.
  • Assuming JSON supports BigInt: JSON.stringify({ big: 1n }) throws. Fix it by serializing BigInt values as strings and parsing them with BigInt().
  • Forgetting the n suffix: 9007199254740993 is a Number and may round. Fix it by writing 9007199254740993n or BigInt("9007199254740993").

When to use it

Use BigInt when you need exact integers beyond the safe integer range. Use Number for ordinary values, decimals, and performance-sensitive code.

NeedUseWhy
IDs larger than 2^53 - 1BigIntExact integer representation
Money with centsNumber or scaled BigIntNumber is fine below the safe limit; BigInt avoids float errors when exactness matters
Decimals like 0.1NumberBigInt only stores integers
High-performance loopsNumberBigInt operations are generally slower

Practice

Guided exercise: create a BigInt from the string "12345678901234567890", add 1n, and log the result.

const value = BigInt("12345678901234567890");
console.log(value + 1n); // 12345678901234567891n

Challenge: compute 2n ** 64n and compare it with Number.MAX_SAFE_INTEGER. Hint: use console.log() and a relational operator.

const big = 2n ** 64n;
console.log(big > Number.MAX_SAFE_INTEGER); // true

Quick check

Question: Why does 9007199254740993n + 1n work exactly, but 9007199254740993 + 1 may not give the exact expected integer?

Answer: The first uses BigInt, which stores exact arbitrary-precision integers. The second uses Number, whose safe integer range ends at 9007199254740991, so larger integers can be rounded.

Summary

BigInt gives JavaScript exact whole-number arithmetic beyond the safe integer limit. Use it for large IDs, exact integer math, and values that must not silently round, while keeping Number for decimals and ordinary performance-sensitive calculations.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

BigInt – FAQs

Quick answers about learning BigInt in JavaScript.

This free note from Coding Now Tech Institute explains BigInt in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including BigInt, is 100% free with no signup required.
With focused practice, most students grasp BigInt in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now