DEV Community

loading...

Convert a number to an array of digits

Tizhad
・1 min read

Solution 1:

const number = 37629;
const digits = (number) => [...`${number}`].map((i) => parseInt(i));
console.log(digits(number));
Enter fullscreen mode Exit fullscreen mode

Solution 2:

const digits = (number) => [...`${number}`].map((i) => parseInt(i));
console.log(digits(37629));
Enter fullscreen mode Exit fullscreen mode

You should see this result in output:

Alt Text

Discussion (0)