BigInt
A type for exact calculations on arbitrarily large integers. Literals end with the letter n.
- Returns
- A value of type
bigint.
Example
#const big = 9007199254740993n
console.log(9007199254740993)
console.log(big)
console.log(big + 2n)
console.log(typeof big)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
#123n
BigInt(value)Parameters
#value
integer or string
The value converted toBigInt. A number with a fractional part throwsRangeError, and a string that isn’t an integer throwsSyntaxError.
More examples
#const max = Number.MAX_SAFE_INTEGER
console.log(max)
console.log(max + 1 === max + 2)
console.log(BigInt(max) + 1n === BigInt(max) + 2n)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)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)))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_INTEGERas strings orBigInt. Converting them to an ordinary number silently changes the last digits. - Before sending data as JSON, convert
BigIntto a string, e.g. withvalue.toString().JSON.stringify()doesn’t support this type and throws aTypeError. BigInthas no fractional part. Store money amounts in cents, and remember that division always truncates the result to an integer.
Related entries
#- Number.isInteger()Checks whether a value is an integer.
- typeofReturns a string with the name of a value’s type, e.g. "number", "string", or "object".
- Number.prototype.toString()Converts a number into a string, also in a different numeral system, e.g. binary or hexadecimal.
- JSON.stringify()Converts a JavaScript value, such as an object or array, into a string in JSON format.
- Math.pow()Raises a number to a power.
Spotted a mistake or missing an example? Write to us.