ES10 feature - Bigint type

ES10 introduced Bigint new datatype to latest new java feature.

bigint is of numeric data type,Currently in javascript number is data type. Number can store integer values from 2 to power of 53 values. Current javascript has limitation to store the integers less than than 2 power 53.

In JavaScript, Number has double precision floats values that can store maximum values of 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
console.log(maxNumber+' = '+typeof maxNumber) //9007199254740992 = number
console.log(maxNumber+' = '+typeof maxNumber) // 9007199254740992 = number

The above program gives an invalid value for second time incrementing 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 not able to accommodate to number type.

Prior to ES10, to accommodate bigger values than 2 power 53, we have to use either third party libraries.

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

Bigint is 7th new data type of storing arbitrary data apart from Number,String,null,undefined,

Important points

  • New numeric data type introduced in javascript
  • Stores numeric arbitrary of 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 can be concatenated with big numbers
  • Decimals are ignored, It allows to have BigDecimal type in future release
  • JSON stringfy not supported

Syntax Big integer contains integer value appending n to an end. It cane 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 +,-,*,/ % can be applied in Biginteger.

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 returns number with fraction of numbers, but returns number 3n as it is BigInt not BigDecimal Fraction of digits are rounded to 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 biginter type value

In the below program like normal data type, typeof operator can be applied on BigInt. typeof returns type of the object data. typeof Object(123n) is not BigInt, returns 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