# 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)); // 
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)); // 
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)); // 
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)); // 
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)); // 
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)); // 
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)); // 
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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5095893,"math_prob":0.9816434,"size":4351,"snap":"2023-40-2023-50","text_gpt3_token_len":1110,"char_repetition_ratio":0.16724178,"word_repetition_ratio":0.3138138,"special_character_ratio":0.29188693,"punctuation_ratio":0.19428572,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99643403,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-06T18:38:06Z\",\"WARC-Record-ID\":\"<urn:uuid:cff44ffc-0a0a-4344-9920-6c78f7448e5c>\",\"Content-Length\":\"520191\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dde4583a-6079-4443-b820-ec1aa7f499dd>\",\"WARC-Concurrent-To\":\"<urn:uuid:962e0313-6ea6-4df6-b826-82865bca8a11>\",\"WARC-IP-Address\":\"172.67.176.28\",\"WARC-Target-URI\":\"https://www.designcise.com/web/tutorial/how-to-convert-a-positive-integer-to-an-array-of-digits-in-javascript\",\"WARC-Payload-Digest\":\"sha1:LCMHGCBXQIDXZPL7U5YJV775R5VE2X2N\",\"WARC-Block-Digest\":\"sha1:22ZICSIBLQKKCEC7LIYZYJVMUE2FC35Z\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100602.36_warc_CC-MAIN-20231206162528-20231206192528-00231.warc.gz\"}"}