You probably won't run into BigInt for a while, and for most everyday code, that's completely fine. Regular numbers in JavaScript handle almost everything you'll ever need. But every so often, you'll hit a number so large that your math quietly starts giving you wrong answers, and no error message tells you it happened. That's exactly the gap BigInt was built to fill.
Let's go through what it actually is, why regular numbers break down at a certain point, and how to use BigInt without tripping over its few quirks.
Table of Contents
Why Regular Numbers Aren't Always Enough
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992 console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992 (wrong!)See the problem? Adding 1 and adding 2 gave the exact same result. Past a certain size, JavaScript literally can't tell numbers apart anymore, and it won't warn you when this happens. If your code deals with things like large IDs, precise financial totals, or cryptography, this silent inaccuracy is a real problem, not just a theoretical one.
Creating a BigInt
You can create one of two ways, either by adding an n at the end of a number, or by calling BigInt() directly.let big1 = 9007199254740993n; let big2 = BigInt(9007199254740993); console.log(typeof big1); // "bigint" console.log(big1); // 9007199254740993nThat trailing n is how JavaScript tells a BigInt apart from a regular number when it's printed, and it's not optional when writing one directly in your code.
Now the Big Numbers Actually Work
With BigInt, that earlier problem disappears completely.let big = 9007199254740991n; console.log(big + 1n); // 9007199254740992n console.log(big + 2n); // 9007199254740993nNo more silent rounding. Each value is tracked precisely, no matter how large it gets.
The Rule Everyone Forgets: You Can't Mix Types
This is the one that catches almost everyone the first time. You can't perform math between a BigInt and a regular number directly.let big = 10n; let regular = 5; console.log(big + regular); // TypeError: Cannot mix BigInt and other typesJavaScript refuses to guess which type you actually want, so it just throws an error instead. To fix it, convert one side to match the other.
console.log(big + BigInt(regular)); // 15n console.log(Number(big) + regular); // 15Just know that converting a BigInt back to a regular Number can lose precision again if the value is large enough, so only do that once you're back in a safe range.
Comparisons Still Work Fine
Even though you can't mix types in math, comparing a BigInt to a regular number works exactly as you'd expect.console.log(10n === 10); // false (different types) console.log(10n == 10); // true (loose equality allows it) console.log(10n > 5); // true
The strict === check cares about type, so it says false. Loose equality and comparison operators like > don't care about type, so they work across both without any conversion needed.
Conclusion
BigInt isn't something you'll reach for every day, and that's exactly how it's meant to be used, as a specific tool for a specific problem. Once your numbers get big enough that precision actually matters, financial systems, cryptography, huge unique IDs, this is what keeps your math honest instead of quietly rounding in the background. Just remember the one real rule, don't mix it with regular numbers in the same operation, and you'll rarely run into trouble.Frequently Asked Questions (FAQs)
1.Do I need to use BigInt for normal, everyday numbers?2.Why does JavaScript throw an error instead of just converting types automatically?No, regular numbers handle everyday math perfectly well. BigInt is only necessary once you're working with values beyond Number.MAX_SAFE_INTEGER.
3.Can I use decimals with BigInt?Because converting silently could quietly lose precision without you noticing, which defeats the entire purpose of using BigInt in the first place. The error forces you to make that choice on purpose.
No, BigInt only works with whole numbers. Trying to write something like 3.14n will throw a syntax error, since decimals fall outside what BigInt is designed to represent.
0 Comments