BigInt

A type for exact calculations on arbitrarily large integers. Literals end with the letter n.

Returns
A value of type bigint.
On this page

Example

#
JavaScript
const big = 9007199254740993n

console.log(9007199254740993)
console.log(big)
console.log(big + 2n)
console.log(typeof big)
Console
9007199254740992
9007199254740993n
9007199254740995n
bigint

The ordinary number 9007199254740993 lies outside the range of exact numbers, so JavaScript stored it as the nearest possible value, 9007199254740992.

Definition and usage

#

Ordinary numbers in JavaScript (the number type) are exact only up to Number.MAX_SAFE_INTEGER, which is 9007199254740991. Beyond that limit they lose precision and neighboring integers start to blur together. BigInt is a separate primitive type that stores integers of any size without losing accuracy.

You create a BigInt value by appending n to a number (10n), or with the BigInt() function, which accepts an integer or a string of digits. The usual operators +, -, *, **, and % work, and division / truncates the fractional part. typeof 10n returns "bigint".

You can’t mix types in a single operation: 10n + 1 throws a TypeError, so the other value has to be explicitly converted with BigInt() or Number(). BigInt doesn’t work with Math functions, and JSON.stringify() throws an error for it. Comparisons, such as 10n > 5 or 2n == 2, work across types, but 2n === 2 returns false, because the types differ.

Syntax

#
Syntax
123n
BigInt(value)

Parameters

#
  • value

    integer or string

    The value converted to BigInt. A number with a fractional part throws RangeError, and a string that isn’t an integer throws SyntaxError.

More examples

#
The boundary of safe integers
JavaScript
const max = Number.MAX_SAFE_INTEGER

console.log(max)
console.log(max + 1 === max + 2)
console.log(BigInt(max) + 1n === BigInt(max) + 2n)
Console
9007199254740991
true
false
Mixing types and division
JavaScript
try {
  console.log(10n + 1)
} catch (error) {
  console.log(error.name)
}

console.log(10n + BigInt(1))
console.log(7n / 2n)
console.log(Number(7n) / 2)
Console
TypeError
11n
3n
3.5
Large results without losing digits
JavaScript
function factorial(n) {
  let result = 1n
  for (let i = 2n; i <= n; i++) {
    result *= i
  }
  return result
}

console.log(factorial(25n))
console.log(Number(factorial(25n)))
Console
15511210043330985984000000n
1.5511210043330986e+25

Browser support

#

Widely available · since 2020

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Keep IDs from databases and APIs that exceed Number.MAX_SAFE_INTEGER as strings or BigInt. Converting them to an ordinary number silently changes the last digits.
  • Before sending data as JSON, convert BigInt to a string, e.g. with value.toString(). JSON.stringify() doesn’t support this type and throws a TypeError.
  • BigInt has no fractional part. Store money amounts in cents, and remember that division always truncates the result to an integer.

Related entries

#

Spotted a mistake or missing an example? Write to us.