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
- Represents only integers (no fractional part).
- Can store numbers that go beyond the safe range of
number(Number.MAX_SAFE_INTEGER= 2^53 - 1). - Has its own syntax:
bigintliterals end with the lettern.
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 number2. Via the BigInt() constructor:
javascript
let bigFromString: bigint = BigInt("9007199254740993000000000000000");
let bigFromNumber: bigint = BigInt(123);Comparison with the number type
| Characteristic | number | bigint |
|---|---|---|
| Range | up to +-9,007,199,254,740,991 (2^53 - 1) | No limits |
| Number type | Floating-point (float) | Integers only |
| Performance | Faster | Slower |
| Arithmetic | Can mix fractional and integer values | Integers only |
| Compatibility | Works with most JS APIs | Some APIs do not support it |
| Mixing types | Cannot mix with bigint | Error 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 bigintExamples 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); // 0nImportant limitations
bigintcannot be used withMath:
javascript
Math.sqrt(16n); // Errorbigintcannot be mixed withnumberdirectly:
javascript
10n + 5; // Error- For comparison (
<,>,==) it works, but===requires matching types:
javascript
5n == 5; // true
5n === 5; // falseWhen to use bigint
Use bigint when:
- You need to work with large integers that exceed the
numberrange:
- cryptography, hashing
- financial calculations with large sums
- working with identifiers (ID, timestamp)
- counters that could overflow
- 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); // 9007199254740993nShort Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.