Course contentsShow
← JavaScript
Lesson 1970 of 2,72341. Modern JavaScript Features (ES2020+)Free lesson

BigInt and Number Cannot Mix Directly

Understand type coercion limitations between BigInt and Number in arithmetic expressions.

BigInt and Number Cannot Mix Directly

You'll learn why JavaScript prevents direct arithmetic between BigInt and Number types, and how to handle this limitation.

When working with BigInt values, you might expect them to play nicely with regular numbers in arithmetic expressions. However, JavaScript deliberately prevents this mixing to avoid ambiguity and precision loss.

The Type Coercion Problem

Unlike how JavaScript freely converts between many types, BigInt and Number are kept strictly separate. You cannot directly add, subtract, multiply, or divide a BigInt with a regular Number:

const bigNum = 100n;
const regularNum = 50;

// ❌ This throws a TypeError
const result = bigNum + regularNum;
// TypeError: Cannot mix BigInt and other types

Why the Restriction Exists

Think of BigInt and Number like oil and water—they don't mix because they have fundamentally different properties. Number uses floating-point representation and can lose precision with very large integers. BigInt guarantees exact integer precision. Allowing them to mix automatically would force JavaScript to choose which type wins, potentially causing silent data loss.

The Solution: Explicit Conversion

You must explicitly convert one type to match the other:

const bigNum = 100n;
const regularNum = 50;

// Convert Number to BigInt
const result1 = bigNum + BigInt(regularNum);
console.log(result1); // 150n

// Convert BigInt to Number (careful with large values!)
const result2 = Number(bigNum) + regularNum;
console.log(result2); // 150

Be cautious when converting BigInt to Number—if the BigInt exceeds Number.MAX_SAFE_INTEGER, you'll lose precision, defeating the purpose of using BigInt in the first place.

Key Takeaway: BigInt and Number cannot mix in arithmetic operations without explicit type conversion. This strict separation prevents silent precision loss and forces you to consciously choose which type to work with.