Skip to content

Commit d390c2c

Browse files
committed
Merge branch 'master' of C:/Users/osvaldo/Documents/GitHub/copy/en.javascript.info into fix-miscellaneous
2 parents 3f261c0 + 939c9bc commit d390c2c

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 operations are always silent, never give errors, but if the bigint is too huge and won't fit the number type, then extra bits will be cut off, so we should be careful doing such conversion.
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 `value` to a 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+
So we should use `Number()` to convert a bigint to a number.
60+
````
61+
62+
## Comparisons
63+
64+
Comparisons, such as `<`, `>` work with bigints and numbers just fine:
65+
66+
```js run
67+
alert( 2n > 1n ); // true
68+
69+
alert( 2n > 1 ); // true
70+
```
71+
72+
Please note though, as numbers and bigints belong to different types, they can be equal `==`, but not strictly equal `===`:
73+
74+
```js run
75+
alert( 1 == 1n ); // true
76+
77+
alert( 1 === 1n ); // false
78+
```
79+
80+
## Boolean operations
81+
82+
When inside `if` or other boolean operations, bigints behave like numbers.
83+
84+
For instance, in `if`, bigint `0n` is falsy, other values are truthy:
85+
86+
```js run
87+
if (0n) {
88+
// never executes
89+
}
90+
```
91+
92+
Boolean operators, such as `||`, `&&` and others also work with bigints similar to numbers:
93+
94+
```js run
95+
alert( 1n || 2 ); // 1 (1n is considered truthy)
96+
97+
alert( 0n || 2 ); // 2 (0n is considered falsy)
98+
```
99+
100+
## Polyfills
101+
102+
Polyfilling bigints is tricky. The reason is that many JavaScript operators, such as `+`, `-` and so on behave differently with bigints compared to regular numbers.
103+
104+
For example, division of bigints always returns a bigint (rounded if necessary).
105+
106+
To emulate such behavior, a polyfill would need to analyze the code and replace all such operators with its functions. But doing so is cumbersome and would cost a lot of performance.
107+
108+
So, there's no well-known good polyfill.
109+
110+
Although, the other way around is proposed by the developers of [JSBI](https://github.com/GoogleChromeLabs/jsbi) library.
111+
112+
This library implements big numbers using its own methods. We can use them instead of native bigints:
113+
114+
| Operation | native `BigInt` | JSBI |
115+
|-----------|-----------------|------|
116+
| Creation from Number | `a = BigInt(789)` | `a = JSBI.BigInt(789)` |
117+
| Addition | `c = a + b` | `c = JSBI.add(a, b)` |
118+
| Subtraction | `c = a - b` | `c = JSBI.subtract(a, b)` |
119+
| ... | ... | ... |
120+
121+
...And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them.
122+
123+
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, emulates them closely following the specification, so the code will be "bigint-ready".
124+
125+
We can use such JSBI code "as is" for engines that don't support bigints and for those that do support - the polyfill will convert the calls to native bigints.
126+
127+
## References
128+
129+
- [MDN docs on BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
130+
- [Specification](https://tc39.es/ecma262/#sec-bigint-objects).

0 commit comments

Comments
 (0)