A list,
BigInt is a built-in object that provides a way to represent integers greater than 2^53-1. This was originally the largest Number that could be represented as Number in Javascript. BigInt can represent an arbitrarily large integer. – MDN
As can be seen from the above description:
BigInt
Is used to describeThe integer- It fills the
Number
Represents a vacancy on the integer range
Check compatibility: caniuse
Second, large number problem
In Javascript, all numbers are represented in double-precision 64-bit floating-point format, as defined by the IEEE 754-2008 standard.
Under this standard, the Number type in JS can safely represent only integers between -9007199254740991 (-(2^53-1)) and 9007199254740991(2^53-1). Any integer value outside this range may lose precision.
// The maximum safe integer
console.log(Number.MAX_SAFE_INTEGER); / / - 9007199254740991
// Minimum safe integer
console.log(Number.MIN_SAFE_INTEGER); / / - - 9007199254740991
// Lost precision - exceeds the maximum safe integer
console.log(9999999999999999); / / - 10000000000000000
// Note the last digit
9007199254740992= = =9007199254740993; / / to true
// Lost precision - exceeds the minimum safe integer
const minInt = Number.MIN_SAFE_INTEGER;
console.log(minInt); / / - - 9007199254740991
console.log(minInt - 5); / / - - 9007199254740996
// notice how this outputs the same value as above
console.log(minInt - 4); / / - - 9007199254740996
Copy the code
Iii. Solutions
To address these limitations, some JS developers use the string type to represent large integers. For example, the Twitter API adds the string version ID to the object when responding with JSON. In addition, a number of libraries, such as bignumber.js, have been developed to make it easier to handle large integers.
With BigInt, applications no longer need workarounds or libraries to safely represent integers other than number.max_safe_INTEGER and number.min_safe_INTEGER. It is now possible to perform arithmetic operations on large integers in standard JS without risk of loss of accuracy.
To create BigInt, simply append n to the end of the integer. Comparison:
console.log(9007199254740995n); / / - > 9007199254740995 n
console.log(9007199254740995); / / - 9007199254740996
Copy the code
BigInt basic usage
create
// Add n
console.log(1n); / / - 1 n
// Use the constructor BigInt
console.log(BigInt(1)); / / - 1 n
1n= = =BigInt(1) / / to true
typeof 1n; / / to bigint
Copy the code
When created using the constructor, arguments passed to BigInt() are automatically converted to BigInt:
BigInt(10); / / - > 10 n
BigInt('10'); / / - > 10 n
BigInt(true); / / - 1 n
Copy the code
Data types and values that cannot be converted throw exceptions:
BigInt(10.2); / / > RangeError
BigInt(null); / / > TypeError
BigInt("abc"); / / > SyntaxError
Copy the code
operation
All arithmetic operators except the unary plus (+) operator are available for BigInt
10n + 20n; / / - > 30 n
10n - 20n; / / - - 10 n
+10n; // → TypeError: Cannot convert a BigInt value to a number
-10n; / / - - 10 n
10n * 20n; / / - > 200 n
20n / 10n; / / - 2 n
23n % 10n; / / - > 3 n
10n支那3n; / / - > 1000 n
const x = 10n;
++x; / / - 11 n
--x; / / - > 9 n
Copy the code
The unary plus (+) operator is not supported because some programs may rely on + to always generate an invariant of Number, or throw an exception. Changing the behavior of + also breaks asM.js code.
Of course, when used with the BigInt operand, the arithmetic operator should return BigInt. Therefore, the result of the division (/) operator is automatically rounded down to the nearest integer. Such as:
25 / 10; / / - 2.5
25n / 10n; / / - 2 n
Copy the code
Because implicit conversions can lose information, the operation between BigInt and Number cannot be performed directly:
10 + 10n; / / > TypeError
Math.max(2n.4n.6n); / / > TypeError
Copy the code
Relational operators are not affected by this:
10n > 5; / / to true
Copy the code
If you want to perform arithmetic calculations using BigInt and Number, you first need to determine in which type the operation should be performed. To do this, simply convert the operand by calling Number() or BigInt() :
BigInt(10) + 10n; / / - 20 n
// or
10 + Number(10n); / / - 20
Copy the code
When Boolean meets BigInt, BigInt is treated like Number. In other words, as long as it is not 0n, BigInt is treated as truthy:
if (5n) {
// Here the code block will be executed
}
if (0n) {
// The code block is not executed
}
Copy the code
conclusion
BigInt is a new basic data type that fills the void in JS representation and computation of large numbers, eliminating the need to use libraries in scenarios such as large integer ids.
The main thing to note is that you can’t just take “Number” and “BigInt” directly. You need to explicitly convert one of the two types. In addition, the unary plus (+) operator can no longer be used on BigInt due to compatibility issues.
reference
A Guide to BigInt: JavaScript’s Newest Data Type
🎈 Thank you for reading, please click “like” to leave a comment ~