By adding this maximum value, we can get the expected results.
max + 1; // → 9_007_199_254_740_992 ✅
max + 2; // → 9_007_199_254_740_992 ❌
Number.MAX_SAFE_INTEGER）AAny calculationThe accuracy may be lost.For this reason, we can only rely on the numerical value in the security range.
Number The security integer restriction can also safely store and manipulate large integers.。
chrome 67+Start supporting BigInt. All demo in this article is based on chrome 67.
To create a
BigInt，Add the N suffix after the number,For example,
BigInt(number)The function can be used to convert Numberbecome
BigInt。Let me put it another way,
BigInt(123) === 123n。Let’s use these two technologies to solve the problems we encountered before:
BigInt(Number.MAX_SAFE_INTEGER) + 2n; // → 9_007_199_254_740_993n ✅
We will be two
1234567890123456789 * 123; // → 151851850485185200000 ❌
Look at the two numbers above, the end is 9 and 3, 9*3=27, but the result is 000 at the end, obviously wrong.Let’s use it
1234567890123456789n * 123n; // → 151851850485185185047n ✅
This time we got the right result.
Number Security integer constraintsNot applicable to
BigIntWe can perform the correct integer operation without worrying about losing accuracy.
typeofOperatorThis type is detected：
typeof 123; // → 'number' typeof 123n; // → 'bigint'
BigIntsIt’s a separate type, so a
BigIntIt’s never equal to a
42n !== 42。To compare a
Number，It will be before the comparisonOneConvert to another type or use abstract equal (
1 + 1n; // → TypeError 123 < 124n; // → true
BigIntConstructor and constructor
NumberSimilar：Convert its parameters to
BigInt（As mentioned earlier).If the conversion fails, it throws one
BigInt(123); // → 123n BigInt(1.5); // → RangeError BigInt('1.5'); // → SyntaxError
The two library functions will be enabled
BigIntvalueencapsulationFor signed or unsigned integers, they are limited to specific digits.
BigInt.asIntN(width, value)Take one
width-digitBinary signed integers, and will
width-digitA binary unsigned integer.For example, if you are executing 64 bit arithmetic, you can use these API to maintain an appropriate range:
// Highest possible BigInt value that can be represented as a // signed 64-bit integer. const max = 2n ** (64n - 1n) - 1n; BigInt.asIntN(64, max); → 9223372036854775807n BigInt.asIntN(64, max + 1n); // → -9223372036854775808n // ^ negative because of overflow
Please be careful, as long as we pass it
BigIntMore than 64 integer values (for example, the absolute value is 63 bit + symbol is 1 bits).，There will be a spillover。
BigIntIt can accurately represent 64 bit signed and unsigned integers, which are often used in other programming languages.Two new types of array style,
BigUint64ArrayIt is easier to effectively represent and manipulate the list of these values:
const view = new BigInt64Array(4); // → [0n, 0n, 0n, 0n] view.length; // → 4 view; // → 0n view = 42n; view; // → 42n
BigInt64ArrayMake sure that its value is 64 bits.
// Highest possible BigInt value that can be represented as a // signed 64-bit integer. const max = 2n ** (64n - 1n) - 1n; view = max; view; // → 9_223_372_036_854_775_807n view = max + 1n; view; // → -9_223_372_036_854_775_808n // ^ negative because of overflow
BigUint64ArrayEnsure that these values are64The position is unsigned.