Course contentsShow
← JavaScript
Lesson 130 of 2,7234. Working with NumbersFree lesson

BigInt and Number Cannot Mix

Understand why arithmetic between BigInt and Number throws errors and how to explicitly convert between them.

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 BigInt to Number might lose precision
  • Converting Number to BigInt truncates 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 within Number'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().