🟢 EasyCore JS📖 Theory Question

What is BigInt and when do you need it?

💡

Hint

Arbitrary-precision integers — for values beyond Number.MAX_SAFE_INTEGER (2^53 - 1)

Full Answer

JavaScript's Number type (64-bit float) can only safely represent integers up to 2^53 - 1 = 9,007,199,254,740,991. BigInt handles arbitrarily large integers.

// Problem: precision loss with large integers
9007199254740993 === 9007199254740992; // true! — lost a bit

// BigInt — suffix with n
9007199254740993n === 9007199254740992n; // false ✓

const big = 99999999999999999999999999n;
const sum = big + 1n; // works perfectly

// Can't mix BigInt and Number directly
1n + 1; // TypeError: Cannot mix BigInt and other types
Number(1n) + 1; // 2 — explicit conversion
BigInt(5) + 3n; // 8n — explicit conversion

// Comparison with Number (ok with ==, not ===)
1n == 1;  // true (loose equality)
1n === 1; // false (strict, different types)

// No decimal support
10n / 3n; // 3n — truncates toward zero

// Math methods don't support BigInt
Math.max(1n, 2n); // TypeError
💡 Use BigInt for: large database IDs (64-bit integers from other languages), financial amounts where precision matters, cryptography, and any integer computation beyond 2^53.

More Core JS Questions

🟢 EasyWhat is the difference between var, let, and const?→🟢 EasyExplain closures with a practical example.→🟢 EasyWhat is hoisting in JavaScript?→🟢 EasyExplain the event loop, call stack, and microtask queue.→

Practice this in a timed sprint →

5 free questions, no signup required

âš¡ Start Sprint