BigInt and Number Cannot Mix
You'll learn why JavaScript prevents direct arithmetic between BigInt and Number, and how to convert between them when needed.
Why JavaScript Keeps Them Separate
JavaScript's BigInt and Number types serve different purposes and use completely different internal representations. To prevent accidental data loss or unexpected behavior, JavaScript refuses to mix them in arithmetic operations. If you try, you'll get a TypeError.
Think of it like trying to add apples and oranges without converting them to a common unit first—JavaScript forces you to be explicit about what you want.
What Happens When You Mix
const big = 100n;
const num = 50;
big + num; // ❌ TypeError: Cannot mix BigInt and other types
big * num; // ❌ TypeError
big - num; // ❌ TypeError
JavaScript doesn't automatically convert either type because:
- Converting
BigInttoNumbermight lose precision - Converting
NumbertoBigInttruncates decimals
Explicit Conversion Required
You must explicitly convert one type to match the other:
const big = 100n;
const num = 50;
// Convert Number to BigInt
big + BigInt(num); // ✅ 150n
// Convert BigInt to Number
Number(big) + num; // ✅ 150
Warning: Converting a huge BigInt to Number may result in loss of precision or Infinity.
When to Convert Which Way
- Use
BigInt()when you need exact integer precision - Use
Number()when the value fits safely withinNumber's range and you need to work with regular numbers
Key Takeaway: BigInt and Number cannot be used together in arithmetic—you must explicitly convert one to the other using BigInt() or Number().