Skip to main content

What is the bigint type?

The bigint type is a primitive numeric type designed for working with integers of arbitrary length, that is, numbers so large they do not fit into the standard number type.

It appeared in ECMAScript 2020 (ES11) and has been supported in TypeScript since version 3.2.


Features of the bigint type

  1. Represents only integers (no fractional part).
  2. Can store numbers that go beyond the safe range of number (Number.MAX_SAFE_INTEGER = 2^53 - 1).
  3. Has its own syntax: bigint literals end with the letter n.
javascript
let big: bigint = 123456789012345678901234567890n;

How to declare a variable of type bigint

1. Via a literal with n:

javascript
let id: bigint = 9007199254740993n; // larger than the safe number

2. Via the BigInt() constructor:

javascript
let bigFromString: bigint = BigInt("9007199254740993000000000000000"); let bigFromNumber: bigint = BigInt(123);

Comparison with the number type

Characteristicnumberbigint
Rangeup to +-9,007,199,254,740,991 (2^53 - 1)No limits
Number typeFloating-point (float)Integers only
PerformanceFasterSlower
ArithmeticCan mix fractional and integer valuesIntegers only
CompatibilityWorks with most JS APIsSome APIs do not support it
Mixing typesCannot mix with bigintError on bigint + number

Example:

javascript
let a: number = 10; let b: bigint = 20n; // console.log(a + b); // Error: cannot mix number and bigint console.log(BigInt(a) + b); // Convert number to bigint

Examples of operations with bigint

javascript
let bigA = 123456789012345678901234567890n; let bigB = 10n; console.log(bigA + bigB); // 123456789012345678901234567900n console.log(bigA - bigB); // 123456789012345678901234567880n console.log(bigB ** 5n); // 100000n console.log(bigA % bigB); // 0n

Important limitations

  1. bigint cannot be used with Math:
javascript
Math.sqrt(16n); // Error
  1. bigint cannot be mixed with number directly:
javascript
10n + 5; // Error
  1. For comparison (<, >, ==) it works, but === requires matching types:
javascript
5n == 5; // true 5n === 5; // false

When to use bigint

Use bigint when:

  1. You need to work with large integers that exceed the number range:
  • cryptography, hashing
  • financial calculations with large sums
  • working with identifiers (ID, timestamp)
  • counters that could overflow
  1. You need precision rather than computation speed.

Example of a precision comparison

javascript
let normal = 9007199254740991; // MAX_SAFE_INTEGER console.log(normal + 1); // 9007199254740992 console.log(normal + 2); // 9007199254740992 (precision is lost) let big = 9007199254740991n; console.log(big + 1n); // 9007199254740992n console.log(big + 2n); // 9007199254740993n

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.