BigInt

May 20, 2023

A BigInt is a built-in object in JavaScript that provides a way to represent integers of arbitrary length. It is a numeric data type that can be used to represent values that are larger than the maximum safe integer in JavaScript, which is 2^53 – 1 (9,007,199,254,740,991). The BigInt object was introduced in ECMAScript 2020 and is supported in most modern web browsers.

Purpose

The purpose of the BigInt object is to provide a way to represent integers that are too large to be stored as a regular JavaScript number. In JavaScript, all numbers are represented as 64-bit floating-point values. This means that the maximum safe integer that can be represented as a regular number is 2^53 – 1. Any integer larger than this will be rounded to the nearest representable value, which can lead to inaccuracies in calculations.

BigInts, on the other hand, can represent integers of arbitrary length with full precision. This makes them useful for applications that require very large integers, such as cryptography, scientific computing, and financial modeling.

Usage

To create a BigInt in JavaScript, you can use the BigInt() function. This function takes a numeric value as its argument and returns a BigInt object that represents that value. Here is an example:

const bigNum = BigInt(12345678901234567890);
console.log(bigNum); // 12345678901234567890n

Note the use of the n suffix in the output. This indicates that bigNum is a BigInt object, not a regular number.

You can perform arithmetic operations on BigInts just like regular numbers. However, you need to use the appropriate BigInt-specific operators to avoid issues with precision and rounding. The following BigInt-specific operators are available:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division (returns floor of quotient)
  • %: Modulus (returns remainder)
  • **: Exponentiation

Here is an example of using BigInt-specific operators:

const a = BigInt(12345678901234567890);
const b = BigInt(98765432109876543210);

console.log(a + b); // 111111111111111111100n
console.log(a - b); // -86419753208641975320n
console.log(a * b); // 12193263113702179509637627680294625400n
console.log(a / b); // 0n
console.log(a % b); // 12345678901234567890n
console.log(a ** b); // Infinityn

Note that the division operator returns the floor of the quotient. This is different from regular division, which would return a floating-point value.

BigInts also support comparison operators >, <, >=, <=, ==, and !=. When comparing a BigInt to a regular number, the BigInt will be converted to a regular number before the comparison is made. This can lead to unexpected results if the BigInt is too large to be represented as a regular number.

const a = BigInt(12345678901234567890);
const b = 98765432109876543210;

console.log(a > b); // false
console.log(a < b); // true
console.log(a == b); // true

In the above example, a is equal to b when they are compared as regular numbers, because a is too large to be represented as a regular number and gets rounded.

You can also use BigInts with bitwise operators &, |, ^, and ~. These operators treat BigInts as sequences of bits and perform bitwise operations on them.

const a = BigInt(0b1010);
const b = BigInt(0b1100);

console.log(a & b); // 8n (0b1000)
console.log(a | b); // 14n (0b1110)
console.log(a ^ b); // 6n (0b0110)
console.log(~a); // -11n

The ~ operator inverts the bits of the BigInt and returns the two’s complement representation of the result.

Converting BigInts to and from strings

BigInts can be converted to and from strings using the toString() and BigInt() functions, respectively. The toString() function takes an optional radix argument that specifies the base in which to represent the BigInt. The default radix is 10.

const a = BigInt(12345678901234567890);

console.log(a.toString()); // "12345678901234567890"
console.log(a.toString(2)); // "1010111101111001101000101100110110011111011100000110010010010"
console.log(a.toString(16)); // "acebdf13579"

To convert a string to a BigInt, you can use the BigInt() function. If the string contains a valid numeric value, the function will return a BigInt that represents that value. If the string does not contain a valid numeric value, the function will throw a SyntaxError.

const a = BigInt("12345678901234567890");
const b = BigInt("0b1010111101111001101000101100110110011111011100000110010010010");
const c = BigInt("0xacebdf13579");

console.log(a); // 12345678901234567890n
console.log(b); // 12345678901234567890n
console.log(c); // 12345678901234567890n

Note the use of the 0b and 0x prefixes in the second and third examples to indicate binary and hexadecimal notation, respectively.

Limitations

While BigInts provide a way to represent integers of arbitrary length with full precision, they also have some limitations and trade-offs compared to regular JavaScript numbers. Some of these limitations include:

  • BigInts cannot be used with regular math operators (+, -, *, /, %, **) when one of the operands is a regular number. You need to convert the regular number to a BigInt first using the BigInt() function.
  • BigInts cannot be used with regular comparison operators (>, <, >=, <=, ==, !=) when one of the operands is a regular number. You need to convert the regular number to a BigInt first using the BigInt() function.
  • BigInts cannot be used with regular bitwise operators (&, |, ^, <<, >>, >>>) when one of the operands is a regular number. You need to convert the regular number to a BigInt first using the BigInt() function.
  • BigInts cannot be used with regular division operators (/, %, Math.floor(), Math.ceil(), Math.round()) when one of the operands is a regular number. You need to convert the regular number to a BigInt first using the BigInt() function, and then use the appropriate BigInt-specific division operator (/, %) or function (BigInt.asIntN(), BigInt.asUintN()) to avoid precision issues.
  • BigInts cannot be used as keys in regular JavaScript objects or elements in regular JavaScript arrays. You need to use a Map or a Set instead.

Browser Support

The BigInt object is supported in most modern web browsers, including Chrome, Firefox, Edge, Safari, and Opera. However, it is not supported in Internet Explorer or older versions of Safari.

To check if your browser supports BigInts, you can use the following code:

if (typeof BigInt === "function") {
  console.log("BigInts are supported!");
} else {
  console.log("BigInts are not supported.");
}