← Back to list

How I Discovered the Power of BigInt in JavaScript

The other day, I was solving a problem that required converting a binary number to decimal. Simple enough, right? I wrote a quick one-liner…

Beenish Khan · 2025-02-17 15:36 · 0 claps · 1.5 min read
#js #bigint #conversion #binary #decimal
Open on Medium ↗
Wiki topics: 🌐 · Web Development

How I Discovered the Power of BigInt in JavaScript

The other day, I was solving a problem that required converting a binary number to decimal. Simple enough, right? I wrote a quick one-liner using parseInt:

const binaryStr = "1101001110111100001000000000000000000000000000000000000000000000";
console.log(parseInt(binaryStr, 2));

It worked fine for small binary values. But when I tested it with a really large binary string, the output was wrong. At first, I thought I had made a typo. After double-checking, I realized JavaScript was silently messing up my numbers.

Why JavaScript Betrayed Me (Precision Issues) JavaScript numbers are floating-point, meaning they have a hard limit on how big an integer can be before they start losing accuracy. That limit 2⁵³-1 (9007199254740991). Anything beyond that, and JavaScript starts rounding things off like an overconfident math teacher. That’s exactly what was happening with my binary conversion — the number was just too big.

Enter BigInt: My New Best Friend I needed a way to handle large numbers without precision loss. That’s when I discovered BigInt. Instead of using parseInt, I tried this:

const decimalBigInt = BigInt("0b" + binaryStr);
console.log(decimalBigInt.toString());

Things to Watch Out For

Of course, BigInt isn’t perfect. Here are a few quirks I ran into:

  1. You can’t mix BigInt and Number directly
console.log(BigInt(10) + 5); // TypeError
console.log(BigInt(10) + BigInt(5)); // Works!
  1. No floating-point math
console.log(BigInt(10) / BigInt(3)); // 3 (no decimals!)
  1. JSON doesn’t support BigInt
JSON.stringify({ value: BigInt(123) }); // Uncaught TypeError: Do not know how to serialize a BigInt

If you need to serialize a BigInt, convert it to a string first:

JSON.stringify({ value: BigInt(123).toString() });

Final Thoughts Before this, I never really thought about JavaScript’s number limitations. But running into this bug forced me to dig deeper, and I’m glad I did. BigInt turned out to be the perfect solution for handling large numbers without losing precision.

So next time you’re dealing with big numbers — whether it’s binary conversion, cryptography, or massive financial calculations — BigInt has your back


메타데이터
post_id
a16d6cc5671f
slug
how-i-discovered-the-power-of-bigint-in-javascript-a16d6cc5671f
url
https://medium.com/@vdsnini/how-i-discovered-the-power-of-bigint-in-javascript-a16d6cc5671f
canonical_url
https://medium.com/@vdsnini/how-i-discovered-the-power-of-bigint-in-javascript-a16d6cc5671f
author_url
https://medium.com/@vdsnini
status
ok
fetched_at
2026-07-20 22:59:00