JavaScript BigInt

JavaScript BigInt

JavaScript BigInt

BigInt is a special numeric type in JavaScript used to represent integers larger than Number.MAX_SAFE_INTEGER (2^53 - 1).

🔹 1. Creating a BigInt

There are two ways to create a BigInt:

✅ Using n suffix

const bigNumber = 1234567890123456789012345678901234567890n; console.log(bigNumber); // 1234567890123456789012345678901234567890n

✅ Using BigInt() constructor

const bigNumber2 = BigInt("1234567890123456789012345678901234567890"); console.log(bigNumber2); // 1234567890123456789012345678901234567890n

Use the n suffix for direct literals and BigInt() for dynamic values like user input.

🔹 2. Why Use BigInt?

Supports very large numbers without loss of precision.
Prevents overflow errors that occur with regular Number.
Works well for cryptography, finance, and precise calculations.

🔹 3. Arithmetic Operations with BigInt

BigInt supports basic arithmetic like +, -, *, /, and %.

let a = 1000000000000000000000000n; let b = 500000000000000000000000n; console.log(a + b); // 1500000000000000000000000n console.log(a - b); // 500000000000000000000000n console.log(a * 2n); // 2000000000000000000000000n console.log(a / 3n); // 333333333333333333333333n (Rounded down) console.log(a % 3n); // 1n

Division always rounds down (truncates decimals).

🔹 4. Mixing BigInt with Number (❌ Not Allowed)

JavaScript does not allow mixing BigInt with Number in operations.

console.log(10n + 5); // ❌ TypeError: Cannot mix BigInt and other types

Fix: Convert Number to BigInt before using it.

console.log(10n + BigInt(5)); // ✅ 15n

Or convert BigInt to Number (⚠ but may lose precision!):

console.log(Number(10n) + 5); // ✅ 15

🔹 5. Comparisons with BigInt

BigInt can be compared with regular Number without issues.

console.log(10n > 5); // ✅ true console.log(10n === 10); // ❌ false (Strict equality: different types) console.log(10n == 10); // ✅ true (Loose equality: coerces types)

Use == for comparison, but avoid === unless types match.

🔹 6. BigInt in Boolean Context

  • 0n is falsy (like 0).
  • Any other BigInt is truthy.
if (0n) { console.log("This won't run"); } else { console.log("0n is falsy"); // ✅ 0n is falsy }

🔹 7. Using BigInt with JSON (⚠ Not Supported)

JavaScript JSON.stringify() does not support BigInt.

console.log(JSON.stringify({ value: 10n })); // ❌ TypeError: Do not know how to serialize a BigInt

Solution: Convert BigInt to a String before serialization.

console.log(JSON.stringify({ value: 10n.toString() })); // ✅ {"value":"10"}

🔹 8. When to Use BigInt?

✅ When working with very large integers (cryptography, timestamps, large financial numbers).
✅ When avoiding precision loss from Number.
Do not use BigInt for floating-point calculations (it only handles integers).

🔹 9. Summary

BigInt allows handling numbers beyond Number.MAX_SAFE_INTEGER.
Use n suffix (1234n) or BigInt() constructor.
Arithmetic operations work but mixing with Number needs conversion.
BigInt cannot be used in JSON.stringify().
Great for precision-critical applications.

🚀 Use BigInt when you need precise, large-number calculations!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close