3.6 KiB
BigInt
[recent caniuse="bigint"]
BigInt
is a special numeric type that provides support for integers of arbitrary length.
A bigint is created by appending n
to the end of an integer literal or by calling the function BigInt
that creates bigints from strings, numbers etc.
const bigint = 1234567890123456789012345678901234567890n;
const sameBigint = BigInt("1234567890123456789012345678901234567890");
const bigintFromNumber = BigInt(10); // same as 10n
Math operators
BigInt
can mostly be used like a regular number, for example:
alert(1n + 2n); // 3
alert(5n / 2n); // 2
Please note: the division 5/2
returns the result rounded towards zero, without the decimal part. All operations on bigints return bigints.
We can't mix bigints and regular numbers:
alert(1n + 2); // Error: Cannot mix BigInt and other types
We should explicitly convert them if needed: using either BigInt()
or Number()
, like this:
let bigint = 1n;
let number = 2;
// number to bigint
alert(bigint + BigInt(number)); // 3
// bigint to number
alert(Number(bigint) + number); // 3
The conversion of bigint to number is always silent, but if the bigint is too huge and won't fit the number type, then extra bits will be cut off, causing a precision loss.
The unary plus operator `+value` is a well-known way to convert a `value` to number.
On bigints it's not supported, to avoid confusion:
```js run
let bigint = 1n;
alert( +bigint ); // error
```
Comparisons
Comparisons, such as <
, >
work with bigints and numbers just fine:
alert( 2n > 1n ); // true
alert( 2n > 1 ); // true
As numbers and bigints belong to different types, they can be equal ==
, but not strictly equal ===
:
alert( 1 == 1n ); // true
alert( 1 === 1n ); // false
Boolean operations
When inside if
or other boolean operations, bigints behave like numbers.
For instance, in if
, bigint 0n
is falsy, other values are truthy:
if (0n) {
// never executes
}
Boolean operators, such as ||
, &&
and others also work with bigints similar to numbers:
alert( 1n || 2 ); // 1
alert( 0n || 2 ); // 2
Polyfills
Polyfilling bigints is tricky. The reason is that many JavaScript operators, such as +
, -
and so on behave differently with bigints compared to regular numbers.
For example, division of bigints always returns an integer.
To emulate such behavior, a polyfill would need to replace all such operators with its functions. But doing so is cumbersome and would cost a lot of performance.
So, there's no well-known good polyfill.
Although, the other way around is proposed by the developers of https://github.com/GoogleChromeLabs/jsbi library.
They suggest to use JSBI library calls instead of native bigints:
Operation | native BigInt |
JSBI |
---|---|---|
Creation from Number | a = BigInt(789) |
a = JSBI.BigInt(789) |
Addition | c = a + b |
c = JSBI.add(a, b) |
Subtraction | c = a - b |
c = JSBI.subtract(a, b) |
... | ... | ... |
...And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them.
In other words, this approach suggests that we write code in JSBI instead of native bigints. But JSBI works with numbers as with bigints internally, closely following the specification, so the code will be "bigint-ready".