Bigint literals are not available when targeting lower than es2020

In JavaScript, bigint literals are not available when targeting versions lower than ES2020. The introduction of the bigint type in ES2020 allows for the representation of larger integers than the previous Number type, which is limited to 53 bits of precision for integers.

Before ES2020, you can still work with big integers by using the BigInt constructor function. This function takes a string or number as an argument and returns a BigInt object with the corresponding value. Here are a few examples to illustrate this:

    
// BigInt constructor function
const bigint = BigInt("123456789012345678901234567890");

console.log(bigint); // Output: 123456789012345678901234567890n (n indicates a BigInt)

// You can perform arithmetic operations using BigInts
const sum = bigint + BigInt(9876543210);

console.log(sum); // Output: 123456789012345678901244444100n

// You can also convert a regular number to a BigInt
const regularNumber = 1234567890;

const bigintFromRegularNumber = BigInt(regularNumber);

console.log(bigintFromRegularNumber); // Output: 1234567890n
    
  

Note that when using BigInts, you must append the letter ‘n’ to indicate that the value is a BigInt. This is necessary because JavaScript automatically treats numbers without the ‘n’ as regular Number objects.

Read more interesting post

Leave a comment