Skip to content

bigint

In JavaScript, bigint is a primitive type that represents whole numbers of arbitrary size, so integers past the safe range of number keep every digit. It’s one of JavaScript’s primitive types, distinct from the BigInt wrapper object, whose prototype supplies its methods:

Language: JavaScript
> 9007199254740993n
9007199254740993n
> typeof 1n
'bigint'

Bigints are immutable and compare by value, so === and Object.is() treat two bigints with the same digits as equal. The type was added in ES2020.

bigint Literals and typeof

A bigint literal is an integer followed by the n suffix. Hexadecimal, octal, and binary forms accept the suffix too, and so do numeric separators. A fractional or exponent literal such as 1.5n is a SyntaxError, because the type has no fractions:

Language: JavaScript
> 1_000_000n
1000000n
> [0xffn, 0o17n, 0b101n]
[ 255n, 15n, 5n ]

> 2n ** 100n
1267650600228229401496703205376n

> typeof 0n
'bigint'

The specification sets no size limit for a bigint, and engines cap it only at a very large size, so results such as 2n ** 100n stay exact where a number would round.

Converting to bigint

BigInt(value) converts integers, integer strings, and Booleans. It throws for values with a fractional part and for strings that don’t parse as integers. Unlike Number(), it has no implicit counterpart, since unary + throws on a bigint:

Language: JavaScript
> BigInt(42)
42n
> BigInt("123456789012345678901234567890")
123456789012345678901234567890n
> BigInt(true)
1n

> BigInt(1.5)
Uncaught:
RangeError: The number 1.5 cannot be converted to a BigInt because it is not an integer
    at BigInt (<anonymous>)

Loose equality applies type coercion across the two numeric types, while strict equality never does:

Language: JavaScript
> 1n == 1
true
> 1n === 1
false
> 1n < 2
true

Common bigint Gotchas

Arithmetic operators don’t mix bigint and number operands, so one side needs an explicit conversion first:

Language: JavaScript
> 10n + 1
Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
> 10n + BigInt(1)
11n

Division truncates toward zero and drops the remainder, because the type can’t hold a fraction:

Language: JavaScript
> 7n / 2n
3n
> -7n / 2n
-3n

JSON.stringify() throws on a bigint by default:

Language: JavaScript
> JSON.stringify({ id: 1n })
Uncaught TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)

A replacer function that returns value.toString() for bigints is the usual way to serialize them as strings.

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026