Meet BigInt, New Features of ECMAScript 2020 - The Future and Beyond

BigInt is a native JavaScript object that provides a model of representation of integers larger than, which is the largest number that JavaScript can accurately represent, using the primitive type Number. ECMAScript 2020 - The Future and Beyond.

Syntax:

BigInt’s syntax is:

BigInt(value);

Where:

value: is the numeric value of the object that will be created. This can be a string or an integer.

It is important to note that BigInt() is not used with the new operator.

BigInt is created by simply adding an “n” to the end of a literal integer - 10n - or by calling function BigInt();

const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// 9007199254740991n
const hugeString = BigInt("9007199254740991");
// 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
// 9007199254740991n

BigInt differs from Number in some important matters. For example:

  • It cannot be used with methods on the Math object;
  • It cannot be mixed into operations or any Number instance. They must be handled with the same type.
  • BigInt’s accuracy can be lost by converting values to Number.

If you want to run some tests, you can:

  • Testing with typeof:
typeof 1n === 'bigint'; // true
typeof BigInt('1') === 'bigint'; // true
  • If it is involved in an Object, BigInt will be considered as a normal type of “object”:
typeof Object(1n) === 'object'; // true

You can also perform operations with BigInt or with BigInt involved in an object:

const antigoMaximoInt = BigInt(Number.MAX_SAFE_INTEGER);
// 9007199254740991
const maximoMaisUm = antigoMaximoInt + 1n;
// 9007199254740992n
const oFuturo= antigoMaximoInt + 2n;
// 9007199254740993n, isso funciona agora!
const multiplicando = antigoMaximoInt * 2n;
// 18014398509481982n
const subtraindo = multiplicando – 10n;
// 18014398509481972n
const modulo = multiplicando % 10n;
// 2n
const bigN = 2n ** 54n;
// 18014398509481984n
bigN * -1n
// –18014398509481984n

Note:

The/operator also works. However, if the numbers are BigIntS and not BigDecimalS, the operation will be rounded to zero, which will not bring us any fractional value.

const divisao = 4n / 2n;
// 2n
const arredondado = 5n / 2n;
// 2n, e não 2.5n

It is important to note that BigInt is not strictly the same as a Number, but they can be compared normally.

0n === 0;
// false
0n == 0;
// true
1n < 2;
// true
2n > 1;
// true
2 > 2;
// false
2n > 2;
// false
2n >= 2;
// true

BigInt behaves like a number when it is converted to a Boolean, and can also be used with logical operators, also within a conditional structure.

if (0n) {
    console.log("Olá, você está dentro do IF");
} else {
    console.log("Olá, você está dentro do ELSE");
}
0n || 12n;
// 12n
0n && 12n;
// 0n
Boolean(0n);
//  false
!0n;
// true

Properties:

BigInt.prototype;

Allows the addition of properties to a BigInt object. All BigInt instances are inherited from BigInt.prototype. The prototype object of the BigInt constructor can be modified to affect all instances of BigInt.

#ecmascript #bigint #javascript #ecmascript2020

Meet BigInt, New Features of ECMAScript 2020 - The Future and Beyond
14.90 GEEK