Typescript Bigint

The bigint is a new primitive type in Typescript. It is available only if you target esnext in tsconfig.json. it represents the whole number. It can hold numbers larger than 253 – 1. The BigInt uses the arbitrary-precision arithmetic. This article covers how to use bigInt, the arithmetic operations allowed, how to convert bigint to number and number to bigint, etc.

Defining bigint

There are two ways in which you create bigint.

A bigInt is created by appending n to the end of an integer literal

let big1 = 9007199254740991n;
console.log(typeof(big1))


//******output***
//bigint

or by calling the function Global function BigInt()

let big2 = BigInt(9007199254099);
console.log(typeof(big2))


//******output***
//bigint

Examples

let bigNum= BigInt("9045141578140991");
console.log(bigNum)        //9045141578140991n  

Hex & binary numbers.

let bigHex = BigInt("0xffffffffffffff");  
console.log(bigHex);          //72057594037927935n

const bigBin = BigInt("0b11111111111111111111111111111111111111111111111111111");    
console.log(bigBin)          //9007199254740991n

Max Value

The bigint can handle the large values and not limited as the Number datatype, which has (Number.MAX_SAFE_INTEGER). BigInt is limited by the available memory of the host system

Arithmetic Operations

The BigInt can be used with the following arithmetic operations +*-**%. Bitwise operators like &, | , ^ , ~, << , >>, (except >>> Zero fill right shift) operators. The unary operator + is also not supported.

let bigVar=9007199254740991n;

let b=bigVar+1n;
console.log(b);      //9007199254740992n

b=bigVar*10n;
console.log(b);     //90071992547409910n

b=bigVar/5n;
console.log(b);     //1801439850948198n

The / division operator rounds of the final result to the whole number. For example, dividing 5/2 results in 2 and not 2.5. i.e it is an integer and not decimal.

console.log(4n / 2n);    //2n
console.log(5n / 2n);    //2n  and not 2.5 

bigint can not be mixed with operations where numbers are involved as shown below. Either you convert number to bigint or convert bigint to number.

let numVar=100;
let bigVar= 100n;

console.log(numVar+bigVar);

*** Error *****
Operator '+' cannot be applied to types 'number' and 'bigint'.ts(2365)

Convert Number to BigInt

You can use the BigInt function to convert the number to a bigInt

let numVar= 100;
let bigVar=BigInt(numVar)
console.log(bigVar)
console.log(typeof(bigVar))

//**output
//100n
//bigint

Convert BigInt to Number

You can convert the bigInt to number by using the Number function. Beware of the fact that you may lose precision when you coerce bigint to number.

let bigVar= 100n;
let numVar=Number(bigVar)
console.log(numVar)
console.log(typeof(numVar))

//**output
//100
//number

Comparison operators

The comparison operators work well between bigInt and a number

console.log(1n < 2)     //true
console.log(2n > 1)     //true
console.log(2n > 2)     //false
console.log(2n >= 2)    //true
0n === 0   // false
0n == 0    // true

Summary

The bigint is a primitive type. It is available only if you target esnext in tsconfig.json. It is added so as to support the large number. bigint cannot be used in the operations involving numbers. They must be coerced to do that. Beware of the fact that you may lose precision when you coerce bigint to number. You can make use of comparison operators to compare bigInt with a number.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top