How to Convert an Array of Digits to a BigInt in JavaScript?

In JavaScript, you can convert an array of digits to a bigint value (within or beyond the range of Number.MAX_SAFE_INTEGER) in the following ways:

Looping, Shifting and Adding Digits to the Right

To convert an array of digits to a bigint value, you can use the Array.prototype.reduce() method, in the following way:

// ES10+
array.reduce((accum, digit) => (accum * 10n) + BigInt(digit), 0n);

Passing 0n as the second argument to Array.prototype.reduce() is needed to set the value the accumulator is initialized to (when the callback is called for the first time). This is important as without it a "TypeError: Reduce of empty array with no initial value" error would be thrown for empty arrays.

This works in the following way:

  • An "accumulated" value is passed to the callback (as the first argument), which contains the result of the calculation of previous iteration (or the initial value "0n" at start);
  • By multiplying the "accumulated" value by 10n, each value in the array is widened to the right, to which then the current digit is added (after being converted to bigint). This results in each digit in the array being added to the resulting bigint value one by one;
  • At the end of the iteration, the final (single/reduced) value of accumulator is returned.

For example:

// ES10+
const digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const bigIntVal = digits.reduce((accum, digit) => (accum * 10n) + BigInt(digit), 0n);

console.log(bigIntVal); // 123456789123456789n

This can be visualized like so:

// Iteration#0 (accum = 0n, digit = 1) => (0n * 10n) + 1n = 1n
// Iteration#1 (accum = 1n, digit = 2) => (1n * 10n) + 2n = 12n
// Iteration#2 (accum = 12n, digit = 3) => (12n * 10n) + 3n = 123n
// Iteration#3 (accum = 123n, digit = 4) => (123n * 10n) + 4n = 1234n
// Iteration#4 (accum = 1234n, digit = 5) => (1234n * 10n) + 5n = 12345n
// Iteration#5 (accum = 12345n, digit = 6) => (12345n * 10n) + 6n = 123456n
// Iteration#6 (accum = 123456n, digit = 7) => (123456n * 10n) + 7n = 1234567n
// Iteration#7 (accum = 1234567n, digit = 8) => (1234567n * 10n) + 8n = 12345678n
// Iteration#8 (accum = 12345678n, digit = 9) => (12345678n * 10n) + 9n = 123456789n
// Iteration#9 (accum = 123456789n, digit = 1) => (123456789n * 10n) + 1n = 1234567891n
// Iteration#10 (accum = 1234567891n, digit = 2) => (1234567891n * 10n) + 2n = 12345678912n
// Iteration#11 (accum = 12345678912n, digit = 3) => (12345678912n * 10n) + 3n = 123456789123n
// Iteration#12 (accum = 123456789123n, digit = 4) => (123456789123n * 10n) + 4n = 1234567891234n
// Iteration#13 (accum = 1234567891234n, digit = 5) => (1234567891234n * 10n) + 5n = 12345678912345n
// Iteration#14 (accum = 12345678912345n, digit = 6) => (12345678912345n * 10n) + 6n = 123456789123456n
// Iteration#15 (accum = 123456789123456n, digit = 7) => (123456789123456n * 10n) + 7n = 1234567891234567n
// Iteration#16 (accum = 1234567891234567n, digit = 8) => (1234567891234567n * 10n) + 8n = 12345678912345678n
// Iteration#17 (accum = 12345678912345678n, digit = 9) => (12345678912345678n * 10n) + 9n = 123456789123456789n
// => 123456789123456789n

Convert Array to String and Then to Integer

You can use the Array.prototype.join() method to convert an array to a string, and then call BigInt() wrapper to convert the resulting string to a bigint value:

// ES10+
const digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const bigIntVal = BigInt(digits.join(''));

console.log(bigIntVal); // 123456789123456789n

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.