Suggest an editImprove this articleRefine the answer for “BigInt vs Int”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`BigInt`** is a special numeric type that lets you work with integers of arbitrary length, beyond the 2^53-1 limit that applies to a regular `Number`. **Key point:** `BigInt` cannot be mixed with `Number` directly, an explicit type conversion is required.Shown above the full answer for quick recall.Answer (EN)Image## What `BigInt` is > `BigInt` is a special numeric type > that lets you work with **integers of arbitrary length** (without the 2^53-1 limit). It is created by adding `n` at the end of a number, or by calling the `BigInt()` function. ```javascript const big = 123456789012345678901234567890n; const big2 = BigInt("123456789012345678901234567890"); ``` --- ## The problem with regular numbers (`Number`) In JavaScript, all regular numbers have the type `Number` and are stored in the **IEEE 754 (64-bit floating point)** format. This means JS can **exactly represent only integers up to**: ```javascript Number.MAX_SAFE_INTEGER === 9007199254740991; // 2^53 - 1 ``` If the number is larger, **precision loss** begins. --- ### Example with regular numbers ```javascript const num = 9007199254740991; console.log(num + 1); // 9007199254740992 console.log(num + 2); // 9007199254740992 (an error!) ``` > The numbers "stuck together": JS cannot tell `+1` and `+2` apart. --- ## The solution: BigInt ```javascript const big = 9007199254740991n; console.log(big + 1n); // 9007199254740992n console.log(big + 2n); // 9007199254740993n ``` > BigInt lets you work **with numbers of any length** without losing precision. --- ## How to create a BigInt ```javascript const a = 10n; // literal const b = BigInt(10); // via a function const c = BigInt("9007199254740995"); // from a string ``` > But you cannot use decimal fractions: ```javascript BigInt(1.5); // TypeError ``` --- ## Arithmetic with BigInt The basic operations are supported: ```javascript const x = 5n; const y = 2n; console.log(x + y); // 7n console.log(x - y); // 3n console.log(x * y); // 10n console.log(x / y); // 2n (the fractional part is discarded) console.log(x ** y); // 25n ``` > Division always returns an **integer**, the fractional part is discarded. --- ## You cannot mix `Number` and `BigInt` directly ```javascript const big = 10n; const num = 5; console.log(big + num); // TypeError: Cannot mix BigInt and other types ``` You need to explicitly convert the type: ```javascript console.log(big + BigInt(num)); // 15n // or console.log(Number(big) + num); // 15 ``` --- ## Comparing `Number` and `BigInt` | Feature | `Number` | `BigInt` | |---|---|---| | Type | floating-point number | integer of arbitrary length | | Maximum precision | up to 2^53-1 | unlimited | | Fraction support | yes | no | | Operations | standard | standard, but integers only | | Compatibility | everywhere | since 2020 | | Use with `JSON` | yes | no (an error on `JSON.stringify()`) | --- ## Example of practical use ### Storing large IDs (for example, from a database) ```javascript const userId = 183748917238479182374981273n; ``` ### Cryptography, blockchain, finance ```javascript const balance = 999999999999999999999999999n; const transfer = 250000000000000000000n; console.log(balance - transfer); // works precisely ``` --- ## BigInt and comparisons You can compare it with `Number`: JS will automatically convert the type: ```javascript 10n == 10; // true (loose comparison) 10n === 10; // false (strict, different types) ``` --- ## Example with Math (incompatible) `BigInt` does not work with `Math` functions: ```javascript Math.sqrt(4n); // TypeError ``` > But you can use `Number(bigint)` when needed. --- ## Summary | Criterion | `Number` | `BigInt` | |---|---|---| | Type | floating-point number | integer of any length | | Precision loss | after 2^53-1 | none | | Fraction support | yes | no | | Arithmetic | standard | integers only | | Type conversion | not compatible directly | conversion required | | Use case | for regular numbers | for very large integers | --- ## In short - `BigInt` is a **type for working with large integers**. - It is used when precision must be preserved (identifiers, cryptocurrency, money, counters). - It cannot be mixed with `Number` directly. - It does not support fractional values.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.