BigInt vs Int
What BigInt is
BigIntis 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.
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:
Number.MAX_SAFE_INTEGER === 9007199254740991; // 2^53 - 1If the number is larger, precision loss begins.
Example with regular numbers
const num = 9007199254740991;
console.log(num + 1); // 9007199254740992
console.log(num + 2); // 9007199254740992 (an error!)The numbers "stuck together": JS cannot tell
+1and+2apart.
The solution: BigInt
const big = 9007199254740991n;
console.log(big + 1n); // 9007199254740992n
console.log(big + 2n); // 9007199254740993nBigInt lets you work with numbers of any length without losing precision.
How to create a BigInt
const a = 10n; // literal
const b = BigInt(10); // via a function
const c = BigInt("9007199254740995"); // from a stringBut you cannot use decimal fractions:
BigInt(1.5); // TypeErrorArithmetic with BigInt
The basic operations are supported:
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); // 25nDivision always returns an integer, the fractional part is discarded.
You cannot mix Number and BigInt directly
const big = 10n;
const num = 5;
console.log(big + num); // TypeError: Cannot mix BigInt and other typesYou need to explicitly convert the type:
console.log(big + BigInt(num)); // 15n
// or
console.log(Number(big) + num); // 15Comparing Number and BigInt
| Feature | Number | BigInt |
|---|---|---|
| Type | floating-point number | integer of arbitrary length |
| Maximum precision | up to 2^53-1 | unlimited |
| Fraction support | yes | no |
| Operations | standard | standard, but integers only |
| Compatibility | everywhere | since 2020 |
Use with JSON | yes | no (an error on JSON.stringify()) |
Example of practical use
Storing large IDs (for example, from a database)
const userId = 183748917238479182374981273n;Cryptography, blockchain, finance
const balance = 999999999999999999999999999n;
const transfer = 250000000000000000000n;
console.log(balance - transfer); // works preciselyBigInt and comparisons
You can compare it with Number: JS will automatically convert the type:
10n == 10; // true (loose comparison)
10n === 10; // false (strict, different types)Example with Math (incompatible)
BigInt does not work with Math functions:
Math.sqrt(4n); // TypeErrorBut you can use
Number(bigint)when needed.
Summary
| Criterion | Number | BigInt |
|---|---|---|
| Type | floating-point number | integer of any length |
| Precision loss | after 2^53-1 | none |
| Fraction support | yes | no |
| Arithmetic | standard | integers only |
| Type conversion | not compatible directly | conversion required |
| Use case | for regular numbers | for very large integers |
In short
BigIntis a type for working with large integers.- It is used when precision must be preserved (identifiers, cryptocurrency, money, counters).
- It cannot be mixed with
Numberdirectly. - It does not support fractional values.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.