BigInt in JavaScript - When to use it

Have you ever wondered why there is a bigint type in JavaScript and when to use it?

What's BigInt Anyway?

BigInt is a special type of data in JavaScript designed to handle, well, big integers.

We're talking numbers larger than 2^53 - 1, the largest number JavaScript can accurately represent with the Number type.

BigInt lets you work with numbers as large as you need (within reason, your computer's memory is still a limit).

JavaScript was notoriously bad at handling large numbers, and thanks to Bigint, we can overcome most of these limitations.

How to Use BigInt

Using BigInt is super easy. Just add n at the end of an integer, and voilà, it's a BigInt now. Like this:

const bigNumber = 1234567890123456789012345678901234567890n;

You can also convert a regular number to BigInt using the BigInt():

const anotherBigNumber = BigInt(1234567890123456789012345678901234567890);

The Basics: Normal Numbers vs. BigInt

First off, JavaScript numbers are pretty great for most tasks.

However, we can encounter these limitations as more things like cryptography and AI are used in the JS landscape.

So, as a rule of thumb, you should stick with the usual old numbers you know and love until you know you will deal with massive numbers.

  • Normal Numbers: These are perfect for counting, simple math, and when you're sure your numbers won't turn into astronomical figures. Think looping 100 times, calculating the price after tax, or setting a timer for 5 minutes.
let visitors = 150;
let price = 19.99 * 1.07; // Tax calculation
  • BigInt: Use it when dealing with numbers larger than 2^53 - 1. This is your go-to for handling precise calculations in scientific computing (cryptography and statistics/AI might be good use cases).
let hugeId = 9007199254740992n; // Beyond the Number limit
let preciseScientificCalculation = 123456789012345678901234567890n * 2n;

Practical Examples

  • Using Normal Numbers: Counting website visitors? Stick with a normal number.
let dailyVisitors = 4500;
console.log(dailyVisitors + 50); // 4550, no need for BigInt
  • Switching to BigInt: Calculating the total atoms in a grain of sand?

BigInt has got your back:

let atomsInGrainOfSand = 7_500_000_000_000_000_000n; // That's a lot of zeros
console.log(atomsInGrainOfSand * 100n); // A BigInt calculation

Mixing and Matching

It's worth mentioning that although BigInt and regular Integers aren't strictly equal, you can still use them in your comparisons:

0n === 0; // false
0n == 0; // true

2n > 2; // false

Just be mindful that they are technically different types, and this can be tested with the typeof operator:

typeof 10n // 'bigint'
typeof 10 // 'number'

As a simple rule, use normal numbers for everyday tasks and BigInt when things get astronomically big.

It's all about choosing the right tool for the task at hand.

Whether you're counting sheep or atoms, JavaScript has got you covered, thanks to BigInt!

JSJavaScript
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.