|
4 | 4 | | :--- | |
5 | 5 | | [How `Numbers` Work and Behave in JS](#how-numbers-work-and-behave-in-js) | |
6 | 6 | | [Floating Point (Im)Precision](#floating-point-imprecision) | |
| 7 | +| [The BigInt Type](#the-bigint-type) | |
7 | 8 |
|
8 | 9 | ## How [`Numbers`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) Work and Behave in JS |
9 | 10 |
|
@@ -86,3 +87,49 @@ Readings: |
86 | 87 | - [2ality – JavaScript and more](https://2ality.com/2012/04/number-encoding.html) |
87 | 88 |
|
88 | 89 | - [Dealing with float precision in Javascript](https://stackoverflow.com/questions/11695618/dealing-with-float-precision-in-javascript) |
| 90 | + |
| 91 | +## The BigInt Type |
| 92 | + |
| 93 | +In JavaScript, `BigInt` is a built-in data type introduced in ECMAScript 2020 (ES11) to represent integers of arbitrary precision. Prior to `BigInt`, JavaScript natively supported only 64-bit floating-point numbers, which have a maximum safe integer value of 2^53 - 1. Any integer larger than this limit would lose precision and could lead to incorrect results. |
| 94 | + |
| 95 | +`BigInt` is designed to address this limitation by allowing you to work with integers of any size, limited only by the available memory of the system. It can represent integers with an arbitrary number of digits, and it does not suffer from the same precision issues as regular numbers (often referred to as `Number` type). |
| 96 | + |
| 97 | +Here's how you can create a `BigInt`: |
| 98 | + |
| 99 | +```javascript |
| 100 | +const bigIntNumber = 1234567890123456789012345678901234567890n; |
| 101 | +``` |
| 102 | + |
| 103 | +Note the "n" suffix at the end of the number, which tells JavaScript that this is a `BigInt` literal. If you try to assign a value directly to a variable without the "n" suffix, it will be treated as a regular `Number`, and you might lose precision: |
| 104 | + |
| 105 | +```javascript |
| 106 | +const wrongBigInt = 1234567890123456789012345678901234567890; // This will be treated as a regular Number. |
| 107 | +``` |
| 108 | + |
| 109 | +You can perform various mathematical operations with `BigInt` in the same way you would with regular numbers: |
| 110 | + |
| 111 | +```javascript |
| 112 | +const a = 1234567890n; |
| 113 | +const b = 9876543210n; |
| 114 | + |
| 115 | +const sum = a + b; // 11111111100n |
| 116 | +const difference = b - a; // 8641975320n |
| 117 | +const product = a * b; // 12193263111263526900n |
| 118 | +const quotient = b / a; // 8n (integer division, fractional part is discarded) |
| 119 | +const remainder = b % a; // 690123430n |
| 120 | +``` |
| 121 | + |
| 122 | +However, it's important to note that `BigInt` and regular `Number` cannot be mixed in arithmetic operations. When using `BigInt`, the operands should be `BigInt` as well: |
| 123 | + |
| 124 | +```javascript |
| 125 | +const c = 42; |
| 126 | +const sumOfMixedTypes = a + c; // Error: Cannot mix BigInt and other types, conversion not allowed. |
| 127 | +``` |
| 128 | + |
| 129 | +`BigInt` cannot be used with certain built-in methods like `Math.sqrt`, `Math.sin`, etc., as those methods expect regular `Number` types. For these cases, you might need to convert the `BigInt` to a regular `Number` before performing the operation. |
| 130 | + |
| 131 | +Readings: |
| 132 | + |
| 133 | +- [BigInt - MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) |
| 134 | + |
| 135 | +- [BigInt - JavaScript.Info](https://javascript.info/bigint) |
0 commit comments