What is BigInt and how is it different from Number?
Assesses fundamental understanding of JavaScript conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
BigInt is a built-in JavaScript object (introduced in ES2020) that provides a way to represent whole numbers larger than 2^53 - 1 (the largest number JavaScript can reliably represent with the Number primitive).
Key Differences:
| Feature | Number | BigInt |
|---------|--------|--------|
| Maximum Safe Integer | 2^53 - 1 (9,007,199,254,740,991) | No theoretical limit |
| Syntax | 42 | 42n or BigInt(42) |
| Type | "number" | "bigint" |
| Decimals | Supports decimals | Integer only |
| Math Operations | Works with Math object | Cannot use Math object |
| JSON | Native JSON support | No native JSON support |
| Mixing Operations | N/A | Cannot mix with Number without explicit conversion |
Creating BigInt:
// Using 'n' suffix
const bigInt1 = 9007199254740991n;
const bigInt2 = 123456789012345678901234567890n;
// Using BigInt() function
const bigInt3 = BigInt("9007199254740991");
const bigInt4 = BigInt(9007199254740991);
console.log(typeof bigInt1); // "bigint"
Common Use Cases:
// 1. Large integer arithmetic
const largeNumber = 9007199254740992n;
const result = largeNumber + 1n; // 9007199254740993n
// Problem with Number:
console.log(9007199254740992 + 1); // 9007199254740992 (incorrect!)
console.log(9007199254740992n + 1n); // 9007199254740993n (correct!)
// 2. High-precision calculations
const factorial = (n) => {
if (n === 0n) return 1n;
return n * factorial(n - 1n);
};
console.log(factorial(50n)); // Accurate result for 50!
// 3. Cryptography and unique identifiers
const uniqueId = 1234567890123456789012345n;
// Important: Cannot mix BigInt and Number
const num = 10;
const big = 20n;
// console.log(num + big); // TypeError: Cannot mix BigInt and other types
console.log(BigInt(num) + big); // 30n (correct)
console.log(num + Number(big)); // 30 (correct, but loses precision for large values)
// Comparison works across types
console.log(10n == 10); // true
console.log(10n === 10); // false (different types)
console.log(10n < 15); // true
Limitations:
// No decimal support
const decimal = 3.5n; // SyntaxError
// Cannot use with Math object
Math.sqrt(16n); // TypeError
// JSON serialization requires custom handling
const data = { id: 123n };
JSON.stringify(data); // TypeError: Do not know how to serialize a BigInt
// Solution for JSON:
JSON.stringify(data, (key, value) =>
typeof value === 'bigint' ? value.toString() : value
);
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.