Skip to content

Commit 55574c7

Browse files
committed
bigint
0 parents  commit 55574c7

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

article.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# BigInt
2+
3+
[recent caniuse="bigint"]
4+
5+
`BigInt` is a special numeric type that provides support for integers of arbitrary length.
6+
7+
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.
8+
9+
```js
10+
const bigint = 1234567890123456789012345678901234567890n;
11+
12+
const sameBigint = BigInt("1234567890123456789012345678901234567890");
13+
14+
const bigintFromNumber = BigInt(10); // same as 10n
15+
```
16+
17+
## Math operators
18+
19+
`BigInt` can mostly be used like a regular number, for example:
20+
21+
```js run
22+
alert(1n + 2n); // 3
23+
24+
alert(5n / 2n); // 2
25+
```
26+
27+
Please note: the division `5/2` returns the result rounded towards zero, without the decimal part. All operations on bigints return bigints.
28+
29+
We can't mix bigints and regular numbers:
30+
31+
```js run
32+
alert(1n + 2); // Error: Cannot mix BigInt and other types
33+
```
34+
35+
We should explicitly convert them if needed: using either `BigInt()` or `Number()`, like this:
36+
37+
```js run
38+
let bigint = 1n;
39+
let number = 2;
40+
41+
// number to bigint
42+
alert(bigint + BigInt(number)); // 3
43+
44+
// bigint to number
45+
alert(Number(bigint) + number); // 3
46+
```
47+
48+
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.
49+
50+
````smart header="The unary plus is not supported on bigints"
51+
The unary plus operator `+value` is a well-known way to convert a `value` to number.
52+
53+
On bigints it's not supported, to avoid confusion:
54+
```js run
55+
let bigint = 1n;
56+
57+
alert( +bigint ); // error
58+
```
59+
````
60+
61+
## Comparisons
62+
63+
Comparisons, such as `<`, `>` work with bigints and numbers just fine:
64+
65+
```js run
66+
alert( 2n > 1n ); // true
67+
68+
alert( 2n > 1 ); // true
69+
```
70+
71+
As numbers and bigints belong to different types, they can be equal `==`, but not strictly equal `===`:
72+
73+
```js run
74+
alert( 1 == 1n ); // true
75+
76+
alert( 1 === 1n ); // false
77+
```
78+
79+
## Boolean operations
80+
81+
When inside `if` or other boolean operations, bigints behave like numbers.
82+
83+
For instance, in `if`, bigint `0n` is falsy, other values are truthy:
84+
85+
```js run
86+
if (0n) {
87+
// never executes
88+
}
89+
```
90+
91+
Boolean operators, such as `||`, `&&` and others also work with bigints similar to numbers:
92+
93+
```js run
94+
alert( 1n || 2 ); // 1
95+
96+
alert( 0n || 2 ); // 2
97+
```
98+
99+
## Polyfills
100+
101+
Polyfilling bigints is tricky. The reason is that many JavaScript operators, such as `+`, `-` and so on behave differently with bigints compared to regular numbers.
102+
103+
For example, division of bigints always returns an integer.
104+
105+
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.
106+
107+
So, there's no well-known good polyfill.
108+
109+
Although, the other way around is proposed by the developers of [https://github.com/GoogleChromeLabs/jsbi](JSBI) library.
110+
111+
They suggest to use JSBI library calls instead of native bigints:
112+
113+
| Operation | native `BigInt` | JSBI |
114+
|-----------|-----------------|------|
115+
| Creation from Number | `a = BigInt(789)` | `a = JSBI.BigInt(789)` |
116+
| Addition | `c = a + b` | `c = JSBI.add(a, b)` |
117+
| Subtraction | `c = a - b` | `c = JSBI.subtract(a, b)` |
118+
| ... | ... | ... |
119+
120+
...And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them.
121+
122+
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".
123+
124+
## References
125+
126+
- [MDN docs on BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
127+
- [Specification](https://tc39.es/ecma262/#sec-bigint-objects).

0 commit comments

Comments
 (0)