Last updated: Mar 2, 2024
Reading timeยท3 min
To split a number into an array:
String()
constructor to convert the number to a string.Array.from()
method to convert the string to an array of digits.map()
method to convert each string in the array to a number.const number = 1234; const arrOfDigits = Array.from(String(number), Number); console.log(arrOfDigits); // ๐๏ธ [1, 2, 3, 4]
The first argument we passed to the Array.from() method is an iterable object that we want to convert to an array.
We had to convert the number to a string because numbers are not iterable.
// ๐๏ธ ['1', '2', '3', '4'] console.log(Array.from(String(1234)));
At this point we have an array of strings. We used the second parameter of the
Array.from()
method to convert each string to a number.
The second parameter the method takes is an Array.map() function.
The function gets called with each element in the array. We want to convert each element to a number, so we make use of the Number() constructor.
Array.map()
method manually insteadHere is an equivalent example that explicitly uses the Array.map()
method.
const number = 1234; // ๐๏ธ ['1', '2', '3', '4'] const arrOfStr = Array.from(String(number)); const arrOfNum = arrOfStr.map(str => Number(str)); console.log(arrOfNum); // ๐๏ธ [1, 2, 3, 4]
The function we passed to the Array.map()
method gets called with each string
in the array.
On each iteration, we convert the current element to a number and return the result.
Alternatively, you can use the String.split()
method.
This is a three-step process:
String()
constructor to convert the number to a string.split()
method on the string to get an array of strings.map()
method on the array to convert each string to a number.const number = 1234; const arr = String(number) .split('') .map(str => Number(str)); console.log(arr); // ๐๏ธ [1, 2, 3, 4]
We used the String()
constructor to convert the number to a string and used
the String.split()
method.
The method splits a string into an array based on the provided separator.
const number = 1234; // ๐๏ธ ['1', '2', '3' ,'4'] console.log(String(number).split(''));
split()
method because we wanted to split on each digit.Lastly, we used the map()
method to convert each string to a number.
We could have also passed the Number()
constructor directly to the map()
method to achieve the same result.
const number = 1234; const arr = String(number).split('').map(Number); console.log(arr); // ๐๏ธ [1, 2, 3, 4]
You can imagine that the Number()
constructor gets called with each element of
the array, then converts the value to a number and returns the result.
const arr = ['1', '2', '3', '4']; // ๐๏ธ [ 1, 2, 3, 4 ] console.log(arr.map(Number)); // ๐๏ธ [ 1, 2, 3, 4 ] console.log(arr.map(element => Number(element)));
Note that this approach is a bit more implicit and a general rule of thumb is that being explicit makes your code more readable.
If you have to split a number into an array often, define a reusable function.
function splitNumber(num) { return String(num) .split('') .map(str => Number(str)); } console.log(splitNumber(1234)); // ๐๏ธ [ 1, 2, 3, 4 ] console.log(splitNumber(123)); // ๐๏ธ [ 1, 2, 3 ] console.log(splitNumber(12)); // ๐๏ธ [ 1, 2 ] console.log(splitNumber(1)); // ๐๏ธ [ 1 ]
The splitNumber()
function takes a number as a parameter and splits the number
into an array.
You can learn more about the related topics by checking out the following tutorials: