In this article, we will show you the difference between BigInt and number data types. And also when to use the BigInt data type

Table of Content

## BigInt Vs Number

### BigInt is an integer, number is a decimal

You cannot store 100.20 in a BigInt, because it is an integer and not decimal.

1 2 3 4 5 6 7 8 9 10 | //bigInt var BigNum1=100n; //ok var BigNum2=100.20n; /error //number var Num1=100; //ok var Num2=100.20; /ok |

### They are implemented differently

bigInt is stored as arbitrary-precision integers and the number as double-precision 64-bit number.

### BigInt can handle large numbers

The `number`

can handle numbers up to 9007199254740991 ( `Number.MAX_SAFE_INTEGER`

). It is a limitation imposed due to the double precison 64 bit number

The `BigInt`

can handle numbers larger than that. `BigInt`

is limited by the available memory of the host system.

### BigInt cannot be mixed with numbers

bigint can not be mixed with operations where numbers are involved as shown below.

1 2 3 4 5 6 7 8 9 | let numVar=100; let bigVar= 100n; console.log(numVar+bigVar); *** Error ***** Operator '+' cannot be applied to types 'number' and 'bigint'.ts(2365) |

### Coercing BigInt to number may lose precision

`bigInt`

are converted to a number by using the `Number`

function

1 2 3 4 5 6 | let numVar=100; let bigVar= 100n; console.log(numVar+ Number(bigVar)); //100 |

### You can compare bigInt with a number

You can compare a `bigInt`

with a `number`

.

1 2 3 4 5 6 | console.log(1n < 2) //true console.log(2n > 1) //true console.log(2n > 2) //false console.log(2n >= 2) //true |

### BigInt is slow

The arithmetic operations involving BigInt are slower compared to the Floating Point arithmetic of a primitive number.

The floating-point arithmetic is implemented in hardware, Hence it is faster. The Arbitrary-precision arithmetic is entirely implemented in software hence slower.

## When to use BigInt

- The bigInt is the only option if you need to support numbers larger than Number.MAX_SAFE_INTEGER
- The precision of calculations are more important than the speed

## Summary

In this article, we learned the difference between `BigInt`

and `number`

data type in Typescript.

Brad Duncan“BigInt is an integer number is a decimal” probably meant to say “BigInt is an integer number, not a decimal”

AnonymousOoops, maybe a comma

“BigInt is an integer, number is a decimal”

TekTutorialsHubThank you, guys.