JavaScript BigInt

BigInt is a JavaScript primitive type for representing integers beyond the range where `Number` can represent every integer exactly. It avoids the precision loss that can occur with large integer values.

A BigInt is created by adding `n` to an integer literal, such as `123n`, or by calling `BigInt()` with a suitable integer value. Unlike `Number`, BigInt arithmetic uses integer precision rather than IEEE 754 floating-point representation.

The `Number` type can represent integers exactly only through `Number.MAX_SAFE_INTEGER`, which is `2^53 - 1`. Beyond that boundary, different mathematical integers can map to the same `Number`, so BigInt is useful when exact integer arithmetic is required.

BigInts and Numbers are distinct types. You generally cannot mix them directly in arithmetic expressions such as `1n + 1`; convert one operand first. Comparisons such as `1n === 1` are false because strict equality checks the types, while relational comparisons can compare the numeric values.

BigInt division discards the fractional part because the result must remain an integer. BigInt is useful for values such as large IDs, counters, integer-based calculations, and algorithms where exact integer arithmetic matters, but it is not a general replacement for `Number`.

Key points

Interview questions

Why was BigInt added to JavaScript?

BigInt provides exact integer arithmetic for values that exceed Number's safe integer range. JavaScript Numbers use floating-point representation, so sufficiently large integers cannot all be represented distinctly. BigInt lets code work with arbitrarily large whole numbers without that particular precision problem. It is therefore useful when integer exactness matters.

What happens if you add a BigInt and a Number?

Arithmetic operators generally do not allow BigInt and Number operands to be mixed directly. For example, `1n + 1` throws a `TypeError`. The operands must be deliberately converted to a compatible numeric type first. This restriction helps prevent accidental loss of numeric precision.

How do you create and convert BigInts?

A BigInt literal uses the `n` suffix, such as `42n`. The `BigInt()` function can also create a BigInt from an appropriate integer value or integer-form string. Converting a BigInt to Number is possible with `Number()`, but large values may lose precision. Conversion should therefore be used only when the target representation can safely hold the value.

How does division work with BigInt?

BigInt division produces another BigInt, so it cannot represent a fractional remainder. For example, `5n / 2n` produces `2n`. The fractional part is truncated toward zero. If an application needs a fractional result, it should use an appropriate representation such as Number or explicitly track quotient and remainder.

What are practical frontend use cases and limitations of BigInt?

BigInt can be useful for exact large integer identifiers or counters and for client-side algorithms that require arbitrary-size integer arithmetic. It is not a drop-in replacement for Number because it cannot represent fractions and cannot be freely mixed with Number arithmetic. Developers also need to consider serialization boundaries, because standard JSON does not directly serialize BigInt values. The choice should therefore follow the data's numeric requirements rather than simply its size.