# BigInt-The Newest Datatype in Javascript

js.Srijan

BigInt is the newest datatype in Javascript that will soon be supported by all browsers.

So, why we need a new data type in the first place? Aren't Numbers sufficient for all the use cases?

To answer the above question, let's look at the Number Datatype in little depth.

Numbers in Javascript are represented in a double-precision floating-point 64-bit format as defined in IEEE 754-2008 standard. According to this standard, any number bigger than 2^53 cannot be represented accurately and are rounded off. So the number loses precision. Therefore, the Number data type is not suitable for very large calculations. Javascript provides two constants which stores maximum and minimum supported values of Number type:

• Number.MAX_SAFE_INTEGER (9007199254740991)
• Number.MIN_SAFE_INTEGER (-9007199254740991)
``````//9007199254740991
console.log(Number.MAX_SAFE_INTEGER)
//-9007199254740991
console.log(Number.MIN_SAFE_INTEGER)``````

To solve the above problem, BigInt is introduced in Javascript. BigInt is capable of representing numbers of arbitrary length without losing precision. Thus, it is helpful in many scenarios where large numbers are a necessity.

## Creating BigInt

You can create BigInt by appending n to the end of the number or using the BigInt constructor.

``````let bigNumber1 = 100n;

//output: 100n
console.log(bigNumber1);

let bigNumber2 = BigInt(300);

//output: 300n
console.log(bigNumber2);``````

## Operators that work on BigInt

Addition, multiplication, subtraction, exponentiation, modulus and bitwise operators work in the same way as they work with Numbers.

``````console.log(1n + 2n); // 3n
console.log(2n - 1n); // 1n``````

BigInt does not support unary (+) operator. The reason behind this is that the unary (+) operator is used for implicit typecasting to Numbers in a lot of codebases which will create confusion and misunderstanding.

Division operations are also rounded towards 0 as BigInts are integers and not decimals.

``````//output: 2n
console.log(5n/2n)``````

BigInt is the same as numbers and is truthy unless it is not 0n.

``````if(0n){
//code will not execute
}

if(5n){
//code will execute
}``````

## Operations between bigInt and Number

### Arithmetic Operations

Arithmetic Operations with a mix of BigInt and Numbers are not allowed as information loss could occur during implicit typecasting.

``````//Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
console.log(1n + 2)``````

### Equality

BigInt is not strictly equal to Number as they are not of the same data type.

``````let a = 3n;
let b = 3;
//output: false
console.log(a === b);``````

Though, they are loosely equal to Numbers.

``````let a = 3n;
let b = 3;
//output: true
console.log(a == b);``````

### Comparision

You can compare a number and a BigInt same as two numbers are compared.

``````//output: true
10n > = 4``````

Browser Support BigInt is pretty new to the Javascript language and hence support for it in major browsers may not be uniform right now. You can see which browser supports BigInt at Can I use dot com.