Skip to Content
Course content

104: Numeric Separators and BigInt

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Wait, why can't I just use a regular number for everything?

You might be wondering why we even need a separate BigInt type. For a long time, JavaScript only had the Number type, which uses 64-bit floats. That's great for decimals, but it has a ceiling for integers called Number.MAX_SAFE_INTEGER (which is 9,007,199,254,740,991).

If you try to go beyond that, JavaScript starts losing precision. It's not that the program crashes; it's worse—it just starts rounding your numbers, and your math becomes wrong. Imagine you're building a system to track snowflake IDs or huge blockchain transaction hashes. A tiny rounding error there is a disaster.

const max = Number.MAX_SAFE_INTEGER;
console.log(max + 1); // 9007199254740992
console.log(max + 2); // 9007199254740992 <-- Wait, what? Same result.

By adding an n to the end of the number, you tell JavaScript to treat it as a BigInt, allowing it to grow as large as your computer's memory allows.

How do I actually use BigInts without breaking my code?

The most important thing to remember is that BigInt and Number are different types, and JavaScript refuses to let you mix them in the same operation. If you try to add a BigInt to a regular Number, the engine will throw a TypeError. I've spent way too much time debugging this exact mistake.

You have to be explicit. If you want to do math with both, you need to convert one of them.

const bigAmount = 1000000000000000000n;
const taxRate = 5;

// This will throw an error:
// const total = bigAmount + taxRate; 

// Do this instead:
const total = bigAmount + BigInt(taxRate);
console.log(total); // 1000000000000000005n

Are those underscores in numbers actually doing something?

No, they aren't doing anything to the logic—they're purely for your sanity. These are called Numeric Separators. When you're staring at a number like 1000000000, you have to stop and manually count the zeros to figure out if it's a hundred million or a billion. It's a waste of mental energy.

You can use the underscore _ as a visual comma. JavaScript completely ignores them when the code runs.

// These are identical to the engine:
const oldWay = 1000000000;
const cleanWay = 1_000_000_000;

// You can even use them with BigInts
const hugeNumber = 9_007_199_254_740_991n;

I highly recommend using these whenever you have a constant larger than four or five digits. It makes your code much more readable for the next person (which is usually you in six months).




📋 Practical Task

Exercise: Intergalactic Distance Calculator

You are writing a script to calculate the distance from Earth to a distant star system in meters. Because the distances are astronomical, you must use BigInt to avoid precision loss.

  • Create a constant called LIGHT_YEAR_METERS and assign it the value of 9_460_730_472_580_800n.
  • Create a variable distanceInLightYears and set it to 4_2n (representing Proxima Centauri).
  • Calculate the total distance in meters by multiplying these two values and store it in a constant called totalDistance.
  • Create a regular Number variable called adjustmentFactor and set it to 100.
  • Add the adjustmentFactor to the totalDistance (remember to cast the Number to a BigInt first).
  • Log the final result to the console.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.