BigInt
JavaScript BigInt
There is a specific numeric type, providing support for integers of arbitrary length. It’s known as biting. (有特定的数字类型,支持任意长度的整数。这就是所谓的咬合。)
You can create a bigint by appending n to the end of an integer literal. Another way of creating a bigint is to call the function BigInt, which generates bigints from numbers, strings, and so on. (您可以通过在整数文本的末尾附加n来创建bigint。创建bigint的另一种方法是调用函数BigInt ,它从数字、字符串等生成bigint。)
Let’s see it in action:
const bigint = 12345678901896532147759012345678901234567890n;
console.log(bigint);
const sameBigint = BigInt("12345678901896532147759012345678901234567890");
console.log(sameBigint);
const bigintFromNumber = BigInt(20); // same as 20n
console.log(bigintFromNumber);
Math Operators
Math Operators (数学运算符)
Most of all BigInt is applied as a regular number, like here:
console.log(1n + 3n); // 4
console.log(7n / 2n); // 3
The 7/2 division will return the result rounded towards zero. All the actions with bigints return only bigints. (7/2除法将返回向零舍入的结果。所有带有bigint的操作仅返回bigint。)
Also, regular numbers and bigints can be mixed, like this:
console.log(1n + 2); // Error: Cannot mix BigInt and other types
If it is required, you can convert them using either Number() or BigInt() as follows:
let bigint = 1n;
let number = 3;
// number to bigint
console.log(bigint + BigInt(number)); // 4
// bigint to number
console.log(Number(bigint) + number); // 4
You should be careful with such operations. Although conversions are silent and don’t cause errors, once the bigint is too large and doesn’t fit the number type, then additional bits will be cut off. (您应该小心这种操作。虽然转换是无声的,不会导致错误,但一旦bigint太大并且不适合数字类型,那么额外的位将被切断。)
As you know, the unary plus operator +value is widely used for converting the value to a number. But, it is not supported on bigints. (如您所知,一元加运算符+值广泛用于将值转换为数字。但是, bigints不支持它。)
Comparisons
Comparisons (比较对照)
Comparisons (<, >) operate with numbers and bigints properly.
Here is an example:
console.log(3n > 1n); // true
console.log(3n > 1); // true
Due to the fact that numbers and bigints are of different types, they may be equal == but not strictly equal ===. Here is an example:
console.log(1 == 1n); // true
console.log(1 === 1n); // false
Boolean Operations
Boolean Operations (布尔运算)
Bigints behave like numbers inside if and other boolean operations. For example, inside if, 0n is will bring false, and other values -true:
if (0n) {
// never executed
}
Boolean operations (||, &&) work with bigints like numbers, as follows:
console.log(1n || 2); // 1 (1n is truthy)
console.log(0n || 2); // 2 (0n is falsy)
Polyfills
Polyfills
It can be tricky to polyfill bigints. That’s because most of the JavaScript operators behave differently with bigints compared to regular numbers. Bigints always return bigints. To emulate behavior like that, polyfill is required. It is necessary for analyzing the code and replacing all such operators with the functions. (聚合物填充bigints可能很棘手。 这是因为与常规数字相比,大多数JavaScript运算符对bigints的行为不同。 Bigint总是返回bigint。 为了模拟这样的行为,需要polyfill。 有必要分析代码并将所有此类运算符替换为函数。)
The developers of the JSBI library use big numbers in their methods. They can be used instead of bigints. Their approach suggests writing the code in JSBI rather than native bigints. It works with numbers like with bigints internally. So, the code will be bigint-read. (JSBI库的开发人员在其方法中使用大数字。它们可以代替bigints使用。他们的方法建议使用JSBI而不是本地bigint编写代码。它可以在内部处理像bigints这样的数字。因此,代码将是bigint-read。)