Skip to content

Commit bee9b2d

Browse files
committed
learn about BigInt
1 parent a5d8784 commit bee9b2d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

More-on-Numbers-and-Strings/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| :--- |
55
| [How `Numbers` Work and Behave in JS](#how-numbers-work-and-behave-in-js) |
66
| [Floating Point (Im)Precision](#floating-point-imprecision) |
7+
| [The BigInt Type](#the-bigint-type) |
78

89
## How [`Numbers`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) Work and Behave in JS
910

@@ -86,3 +87,49 @@ Readings:
8687
- [2ality – JavaScript and more](https://2ality.com/2012/04/number-encoding.html)
8788

8889
- [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

Comments
 (0)