BigInt-The Newest Datatype in Javascript

js

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)
1
2
3
4
//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.

1
2
3
4
5
6
7
8
9
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.

1
2
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.

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

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

1
2
3
4
5
6
7
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.

1
2
//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.

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

Though, they are loosely equal to Numbers.

1
2
3
4
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.

1
2
//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.


If you liked this article, please upvote and recommend it to show your support. Feel free to ask any questions in the comments below.


We publish articles on web development and technology frequently. Consider subscribing to our newsletter or follow us on our social channels (twitter, Facebook, LinkedIn).

Share and support us

Share on social media and help us reach more people