Article From:https://www.cnblogs.com/wangmeijian/p/9217352.html

JavaScriptThe basic data class, Number, is a double precision floating-point number, and the maximum security scope it can represent is positive and negative 9007199254740991, that is, the 53 subtraction of 2, and input the Number.MAX_SAFE_INTEGER in the browser console, respectively.Number.MIN_SAFE_INTEGER can check the corresponding maximum / minimum values.

```const max = Number.MAX_SAFE_INTEGER;
// → 9_007_199_254_740_991
// Note: for ease of reading, I use the underscore as a separator to group these numbers into 1000 digits. The digital text separator proposal is correct for common JavaScript numeric characters.```

By adding this maximum value, we can get the expected results.

```max + 1;
// → 9_007_199_254_740_992 ✅```

However, if we increase it again, the result can no longer be fully expressed as JavaScript.`Number`

```max + 2;
// → 9_007_199_254_740_992 ❌```

We’ll find that the results of max+1 and max+2 are the same.As long as we get this particular value in JavaScript, we can not judge whether it is accurate or not.An integer outside the range of the security integer (that is, from`Number.MIN_SAFE_INTEGER`reach`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.

## `BigInt`

`BigInt`It is a new primitive type in JavaScript, which can be used.Arbitrary precisionRepresentation of integersUse`BigInt`，Even beyond JavaScript`Number` The security integer restriction can also safely store and manipulate large integers.

To create a`BigInt`，Add the N suffix after the number,For example,`123`Become`123n`Overall situation`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`Number` Multiplying

```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`BigInt`Instead of:

```1234567890123456789n * 123n;
// → 151851850485185185047n ✅```

This time we got the right result.

`Number` Security integer constraintsNot applicable to`BigInt`Therefore,`BigInt`We can perform the correct integer operation without worrying about losing accuracy.

`BigInt`It is a primitive type in the JavaScript language.Therefore, it can be used`typeof`OperatorThis type is detected

```typeof 123;
// → 'number'
typeof 123n;
// → 'bigint'```

because`BigInt`sIt’s a separate type, so a`BigInt`It’s never equal to a`Number`，For example`42n !== 42`To compare a`BigInt`And a`Number`，It will be before the comparisonOneConvert to another type or use abstract equal (`==`）：

```1 + 1n;
// → TypeError
123 < 124n;
// → true```

### API

Overall situation`BigInt`Constructor and constructor`NumberSimilar`：Convert its parameters to`BigInt`（As mentioned earlier).If the conversion fails, it throws one`SyntaxError`Or`RangeError`Abnormal.

```BigInt(123);
// → 123n
BigInt(1.5);
// → RangeError
BigInt('1.5');
// → SyntaxError```

The two library functions will be enabled`BigInt`valueencapsulationFor signed or unsigned integers, they are limited to specific digits.`BigInt.asIntN(width, value)`Take one`BigInt`valuePackingFor one`width`-digitBinary signed integers, and will`BigInt.asUintN(width, value)`One`BigInt`valuePackingFor one`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`BigInt`More than 64 integer values (for example, the absolute value is 63 bit + symbol is 1 bits).，There will be a spillover

`BigInt`It can accurately represent 64 bit signed and unsigned integers, which are often used in other programming languages.Two new types of array style,`BigInt64Array`And`BigUint64Array`It 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[0];
// → 0n
view[0] = 42n;
view[0];
// → 42n```

`BigInt64Array`Make 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[0] = max;
view[0];
// → 9_223_372_036_854_775_807n
view[0] = max + 1n;
view[0];
// → -9_223_372_036_854_775_808n
//   ^ negative because of overflow```

`BigUint64ArrayEnsure that these values are`64The position is unsigned.