Skip to content

BigInt

The BigInt global object converts values to arbitrary-precision integers and provides the static and prototype methods that every bigint value uses. The values themselves are the bigint primitive type, and calling BigInt() as a function is how code turns a number or an integer string into one:

Language: JavaScript
> BigInt("9007199254740993")
9007199254740993n
> Object.getPrototypeOf(5n) === BigInt.prototype
true

BigInt is an ECMAScript built-in, added in ES2020. It’s Baseline Widely available, and browsers have shipped it since September 2020.

Syntax

The BigInt function takes one value to convert:

Language: JavaScript Syntax
BigInt(<value>)
Parameter Description
value A required integer number, integer string, Boolean, or bigint. Strings can use decimal, 0x, 0o, or 0b notation

The call returns a bigint primitive. It throws a RangeError for a number that isn’t an integer, including NaN and Infinity, a SyntaxError for a string that isn’t an integer, and a TypeError for undefined, null, or a symbol. The new keyword is forbidden, so new BigInt() throws a TypeError.

Key Static Methods and Properties

Member Kind Description
BigInt.asIntN() Static method Wraps a bigint to the given number of bits and returns it as a signed integer
BigInt.asUintN() Static method Wraps a bigint to the given number of bits and returns it as an unsigned integer

Key Instance Methods and Properties

Member Kind Description
.toString() Instance method Returns the digits as a string in an optional radix from 2 to 36, without the n suffix
.toLocaleString() Instance method Returns a language-sensitive string, such as one with thousands separators
.valueOf() Instance method Returns the primitive bigint that a wrapper object holds

Examples

An API returns 64-bit record IDs as strings, because many of them exceed Number.MAX_SAFE_INTEGER. Passing each string to BigInt() keeps every digit, while Number() rounds it:

Language: JavaScript
> const orderId = "1234567890123456789";
undefined

> BigInt(orderId)
1234567890123456789n
> Number(orderId)
1234567890123456800

A hash function needs 64-bit unsigned arithmetic that wraps on overflow, the way it does in C. Bigint math never overflows, so BigInt.asUintN() truncates each result to 64 bits:

Language: JavaScript
> const maxUint64 = 2n ** 64n - 1n;
undefined

> maxUint64 + 1n
18446744073709551616n
> BigInt.asUintN(64, maxUint64 + 1n)
0n

> BigInt.asUintN(64, -1n)
18446744073709551615n
> BigInt.asIntN(64, 2n ** 63n)
-9223372036854775808n

To see which bits survive the wrap, type a bigint and drag the bit width to compare BigInt.asUintN() and BigInt.asIntN() side by side:

Interactive diagram — enable JavaScript to view.

A dashboard shows the same counter in hex for logs and with separators for people. The two string methods handle both:

Language: JavaScript
> const bytesSent = 1234567890123456789n;
undefined

> bytesSent.toString(16)
'112210f47de98115'
> bytesSent.toLocaleString("en-US")
'1,234,567,890,123,456,789'

Common Gotchas

Passing an unsafe number to BigInt() doesn’t recover lost digits. The number is already rounded before the conversion runs, so the integer string is the safe input:

Language: JavaScript
> BigInt(9007199254740993)
9007199254740992n
> BigInt("9007199254740993")
9007199254740993n

The Math methods accept only numbers, so they throw a TypeError on bigints:

Language: JavaScript
> Math.max(1n, 2n)
Uncaught TypeError: Cannot convert a BigInt value to a number
    at Math.max (<anonymous>)

Comparison operators work on bigints, so a > b ? a : b replaces Math.max() for two values. Mixing bigints and numbers in arithmetic and serializing bigints with JSON.stringify() are covered in the bigint entry.

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. 7, 2026