Skip to main content

BigInt vs Int

What BigInt is

BigInt is a special numeric type that lets you work with integers of arbitrary length (without the 2^53-1 limit).

It is created by adding n at the end of a number, or by calling the BigInt() function.

javascript
const big = 123456789012345678901234567890n; const big2 = BigInt("123456789012345678901234567890");

The problem with regular numbers (Number)

In JavaScript, all regular numbers have the type Number and are stored in the IEEE 754 (64-bit floating point) format.

This means JS can exactly represent only integers up to:

javascript
Number.MAX_SAFE_INTEGER === 9007199254740991; // 2^53 - 1

If the number is larger, precision loss begins.


Example with regular numbers

javascript
const num = 9007199254740991; console.log(num + 1); // 9007199254740992 console.log(num + 2); // 9007199254740992 (an error!)

The numbers "stuck together": JS cannot tell +1 and +2 apart.


The solution: BigInt

javascript
const big = 9007199254740991n; console.log(big + 1n); // 9007199254740992n console.log(big + 2n); // 9007199254740993n

BigInt lets you work with numbers of any length without losing precision.


How to create a BigInt

javascript
const a = 10n; // literal const b = BigInt(10); // via a function const c = BigInt("9007199254740995"); // from a string

But you cannot use decimal fractions:

javascript
BigInt(1.5); // TypeError

Arithmetic with BigInt

The basic operations are supported:

javascript
const x = 5n; const y = 2n; console.log(x + y); // 7n console.log(x - y); // 3n console.log(x * y); // 10n console.log(x / y); // 2n (the fractional part is discarded) console.log(x ** y); // 25n

Division always returns an integer, the fractional part is discarded.


You cannot mix Number and BigInt directly

javascript
const big = 10n; const num = 5; console.log(big + num); // TypeError: Cannot mix BigInt and other types

You need to explicitly convert the type:

javascript
console.log(big + BigInt(num)); // 15n // or console.log(Number(big) + num); // 15

Comparing Number and BigInt

FeatureNumberBigInt
Typefloating-point numberinteger of arbitrary length
Maximum precisionup to 2^53-1unlimited
Fraction supportyesno
Operationsstandardstandard, but integers only
Compatibilityeverywheresince 2020
Use with JSONyesno (an error on JSON.stringify())

Example of practical use

Storing large IDs (for example, from a database)

javascript
const userId = 183748917238479182374981273n;

Cryptography, blockchain, finance

javascript
const balance = 999999999999999999999999999n; const transfer = 250000000000000000000n; console.log(balance - transfer); // works precisely

BigInt and comparisons

You can compare it with Number: JS will automatically convert the type:

javascript
10n == 10; // true (loose comparison) 10n === 10; // false (strict, different types)

Example with Math (incompatible)

BigInt does not work with Math functions:

javascript
Math.sqrt(4n); // TypeError

But you can use Number(bigint) when needed.


Summary

CriterionNumberBigInt
Typefloating-point numberinteger of any length
Precision lossafter 2^53-1none
Fraction supportyesno
Arithmeticstandardintegers only
Type conversionnot compatible directlyconversion required
Use casefor regular numbersfor very large integers

In short

  • BigInt is a type for working with large integers.
  • It is used when precision must be preserved (identifiers, cryptocurrency, money, counters).
  • It cannot be mixed with Number directly.
  • It does not support fractional values.

Short Answer

Interview ready
Premium

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