logo
post image

Represent Very Large Numbers in Javascript with BigInt

BigInt is a new numeric data type brought in Javascript, which is different from the commonly used Number data type.

The Number Data Type

The Number data type is used to represent numeric values in Javascript. There is no separate data type to represent integers, decimals or floating-point numbers.

This is the data type we use in mathematical operations.

var a = 25;
var b = 10;

var c = a + b - 5;

// 30
console.log(a);

Number data type can include numbers between -(253 -1) and 253 -1).

The BigInt Data Type

If we were to use the Number data type to represent the largest integer it would be 253 -1 or 9007199254740991.

But there are applications that have use-cases for very large numbers — Twitter's Snowflake that creates 64-bit integers (whose maximum value could be 9223372036854775807), financial sectors, high-accuracy timestamps etc. If such large numbers are used in Javascript, errors will creep in.

To handle these situations, the BigInt is a new data type introduced in Javascript. BigInt can handle extremely large numbers. There is no maximum limit defined for a BigInt.

There are 2 ways in which you can create a BigInt :

  • suffix n to the number

    var a = 235325325325353n;
  • use BigInt() function

    var a = BigInt(235325325325353);

Also you cannot initialize BigInt with a non-integer, otherwise an error will be thrown.

// error
var a = BigInt(2.5);

Using Both Numbers and BigInt in Mathematical Expressions

Note that Numbers and BigInt cannot be both used in a Mathematical expression, or else an error will be thrown.

In such situations you will need to convert Numbers to BigInt format by calling the BigInt function.

// throws exception
console.log(1 + 2n);

// works
// prints 3n
console.log(BigInt(1) + 2n);

Comparisons can although be done between a Number and BigInt. Strict comparison === among them will fail. But equality comparison == among them can be done.

// false
console.log(2n === 2);

// true
console.log(2n != 3);

Addition of BigInts

BigInt numbers are added normally using the + operator. The important thing to note here is a BigInt number can only be added to another BigInt number.

var num1 = BigInt(25);
var num2 = 35n;

// 60n
console.log(num1 + num2);

Subtraction of BigInts

Just like addition, subtraction is also same as ordinary numbers using the - operator. One BigInt can only be subtracted from another BigInt.

var num1 = 25n;
var num2 = 35n;

// 10n
console.log(num1 - num2);

Multiplication of BigInts

Multiplication of BigInt are again just like normal numbers * operator. Only two BigInts can be multiplied.

var num1 = 25n;
var num2 = 2n;

// 50n
console.log(num1 * num2);

Divison of BigInts

Divison of BigInt happens with the / operator. Only two BigInts can be divided.

var num1 = BigInt(50);
var num2 = 2n;

// 25n
console.log(num1 / num2);

Comparisons Between BigInts

Comparisons between BigInts can be done with the usual >, <, ==, === operators.

// true
console.log(25n > 20n);

// false
console.log(25n < 20n);

// false
console.log(25n == 20n);

// true
console.log(25n == BigInt(25));

Useful Resources