`BigInt`

is a relatively new addition to JavaScript and many aren't familiar with it. This was my case until recently. While working on the Closest Number puzzle on codingame.com, I found myself having to deal with numbers of sizes as big as 10^{1000}.

As you may know, the largest number that can be reliably represented in JavaScript using the `Number`

primitive is 2^{53} or, for comparison, a bit less than 10^{16}. So `Number`

wasn't anywhere close to cutting it. Thankfully, now, there is `BigInt`

.

`BigInt`

is a built-in object that provides a way to represent whole numbers larger than 2^{53}. There is technically no limit to just how big a BigInt can be, although in practice most JavaScript engine will set their own limit, but it's big, reaaaaaalllyy big.

A `BigInt`

can be created in two ways. You can either simply append the letter `n`

to the end of an integral number or by calling the function `BigInt()`

.

```
const reallyBig = 91234567890123456789n
const alsoQuiteBig = BigInt(91234567890123456789)
const justAsBig = BigInt("91234567890123456789")
```

My solution to the Closest Number puzzle involved getting the difference between two large numbers, but not always knowing which one of the two numbers would be bigger. With regular JavaScript `Number`

you would normally do something like:

```
Math.abs(a - b)
```

And even if `a`

was smaller than `b`

, you would get the absolute value of the difference between `a`

and `b`

as a positive number. The problem with trying to do that with `BigInt`

is that the `Math`

object's methods only work on `number`

s not on `bigint`

s. Solution? Write your own `Math`

class. That's just what I have started doing as I have a feeling I will have to use `BigInt`

s again in the near future.

```
export class Mathb {
static abs(x: bigint): bigint {
return x < 0 ? -x : x;
}
}
```

For now, all I have is the `abs`

method but I plan to add to the class as the need arises.

Have you used `BigInt`

yet? Please share anything interesting you have found about it in the comment section below.

## Comments