How to Convert a Positive Integer to an Array of Digits in JavaScript?

JavaScript does not have a built-in method to convert an integer to an array of integers. Converting positive integers is easy. Therefore, if you don't have a requirement to account for negative integers, and need to only convert positive integers to an array of digits, then you can do the following:

Using Array.from() and Number()

The following steps would convert a positive integer to an array of digits:

  1. Convert the number to a string;
  2. Using Array.from(), convert the numeric string to an array of numeric string characters and then convert those to integers by specifying Number as the second argument to the method (which is called on every element of the array).
// ES6+
function numToArray(num) {
    const numStr = String(num);
    return Array.from(numStr, Number);
}

console.log(numToArray(0)); // [0]
console.log(numToArray(12345)); // [1, 2, 3, 4, 5]

You can make this into a one-liner like so:

// ES6+
const numToArray = (num) => Array.from(String(num), Number);

console.log(numToArray(0)); // [0]
console.log(numToArray(12345)); // [1, 2, 3, 4, 5]

Using String.prototype.split() and Number()

The following steps would convert a positive integer to an array of digits:

  1. Convert the number to a string;
  2. Convert string to an array of numeric string characters using String.prototype.split();
  3. Convert each array item to an integer by passing Number as an argument to Array.prototype.map().
// ES6+
function numToArray(num) {
    const numStr = String(num);
    const arr = numStr.split('');
    return arr.map(Number);
}

console.log(numToArray(0)); // [0]
console.log(numToArray(12345)); // [1, 2, 3, 4, 5]

You can make this into a one-liner like so:

// ES6+
const numToArray = (num) => String(num).split('').map(Number);

console.log(numToArray(0)); // [0]
console.log(numToArray(12345)); // [1, 2, 3, 4, 5]

Using String.prototype.match()

The following steps would convert a positive integer to an array of digits:

  1. Convert the number to a string;
  2. Convert string to an array of numeric strings using a regular expression with String.prototype.match();
  3. Return empty array when no match is found (i.e. when String.prototype.match() returns null). Otherwise, use Array.prototype.map() to convert each array item to an integer, by passing Number as an argument to it.
// ES6+
function numToArray(num) {
    const strArr = String(num).match(/\d/g);
    return Array.isArray(strArr) ? strArr.map(Number) : [];
}

console.log(numToArray(0)); // [0]
console.log(numToArray(12345)); // [1, 2, 3, 4, 5]

Please note that the g flag used with match is important here because it returns all results matching the regular expression. Starting with ES11, you could also use String.prototype.matchAll() instead.

Looping and Adding Each Digit to the Array

The following steps would convert a positive integer to an array of digits:

  1. Convert the number to a string;
  2. Loop over each character in the string, converting each one to an integer and adding it to an array one by one.
function numToArray(num) {
    const numericStr = String(num);
    const numbersArr = [];

    for (let i = 0; i < numericStr.length; i++) {
        numbersArr.push(parseInt(numericStr[i], 10));
    }

    return numbersArr;
}

console.log(numToArray(0)); // [0]
console.log(numToArray(12345)); // [1, 2, 3, 4, 5]

If you don't wish to convert the number to a string, then you can do the following:

  1. Determine the length of the integer;
  2. Loop and add the last digit of the integer to the same position in a new array;
  3. Chop off the last digit from the integer, and repeat till there are no digits remaining.
// ES6+
function numToArray(num) {
    // get length of number
    let i = (num === 0) ? 1 : Math.ceil(Math.log10(Math.abs(num) + 1));
    const numbersArr = [];

    do {
        // add number at same position in the new array
        numbersArr[--i] = num % 10;
        // take last digit off `num`
        num = Number.parseInt(num / 10, 10);
    } while (num !== 0);

    return numbersArr;
}

console.log(numToArray(0)); // [0]
console.log(numToArray(12345)); // [1, 2, 3, 4, 5]

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.