ES10 feature - Bigint type

ES10 introduced Bigint’s new datatype to latest new java feature.

Currently in javascript number is a data type. The number can store integer values from 2 to power of 53 values. Current javascript cannot store integers less than 2 power 53.

bigint is of numeric data type,

In JavaScript, a Number has double-precision floats values that can store maximum values of the Number.MAX_SAFE_INTEGER-1 Number.MAX_SAFE_INTEGER is equal to 2 power 53 -1. i.e. 253-1 ( is exponent operator in ES7 )

let maxNumber = Number.MAX_SAFE_INTEGER;
console.log(maxNumber+' = '+typeof maxNumber) // 9007199254740991 = number
maxNumber=maxNumber+1
console.log(maxNumber+' = '+typeof maxNumber) //9007199254740992 = number
maxNumber=maxNumber+1
console.log(maxNumber+' = '+typeof maxNumber) // 9007199254740992 = number

The above program gives an invalid value for the second time incrementing the value above example printed 9007199254740991 as maximum value and type is number.

Incremented its value and give the 9007199254740992 and type is number, as expected. if incremented again, The output is invalid 9007199254740993 is correct. but 9007199254740993 is not able to accommodate to number type.

Before ES10, to accommodate bigger values than 2 power 53, we have to use either third-party library.

With ES10, Bigint was introduced in native javascript to solve the storing of large numbers.

Bigint is the 7th new data type of storing arbitrary data apart from Numbers, Strings, null, undefined,

Important points:

  • New numeric data type introduced in JavaScript
  • Stores numeric arbitrary length
  • Used in mathematic computations of larger numbers and cryptography apps
  • Arithmetic operators can be applied
  • Unary operators are not applied
  • It can not be computed with numbers
  • Strings concatenates with big numbers
  • Decimals are ignored, It allows to have BigDecimal type in a future release
  • JSON stringify is not supported.

Syntax Big integer contains integer value appending n to an end. It can be created using either BigInt constructor or inline creation and assign value

let bigIntegerValue=117823928394820384239n;
let bigIntegerValue1=new BigInt(12312312123123n)
let bigIntegerValue2=new BigInt(5675675);// equals 5675675n
let bigIntegerValue3=new BigInt("77878");

Arithmetic mathematical operators

All the arithmetic operators like +,-, *, and/ % can be applied in Big integer.

console.log(1n + 2n); //3n
console.log(10n - 2n); //8n
console.log(10n * 2n);//20n
console.log(10n / 3n); //3n
console.log(10n % 2n); //on

1n + 2 throws compilation error TypeError: Cannot mix BigInt and other types, use explicit conversions. 10n / 3n gives 3n as / operators expected to return numbers with a fraction of numbers but returns number 3n as it is BigInt not BigDecimal. The fraction of digits rounds to the nearest number.

Comparison, equality operator

Comparison operators (<,> <=,>=) or equality === or == can be compared BigInt with Number

console.log(1n === 1); //false
console.log(1n == 1); //true
console.log(5n < 1); // false
console.log(5n < 1n); //false
console.log(5n > 1); // true
console.log(5n > 1n); //true

Boolean operators

Boolean operators like || and && and conditional expressions are applied

console.log(Boolean(12n)); //true
console.log(Boolean(0n)); //false
console.log(0n || 34n); //34n
console.log(0n && 34n); //0n

How to check big integer type value

In the below program like normal data type, the type of operator can be applied on BigInt. typeof returns the type of the object data. typeof Object(123n) is not BigInt, returns an object

let bigIntegerValue=117823928394820384239n;
let numberVal=123;
let obj=new Object(123n);
console.log(typeof bigIntegerValue) //outputs bigint
console.log(typeof numberVal)//outputs number
console.log(typeof obj)// outputs object

Reference Documentation tc39