how to turn number to array of digits in javascript code example
Example 1: turn number into array javascript
const myNumber = 1245;
function numberToArray(number) {
let array = number.toString().split("");//stringify the number, then make each digit an item in an array
return array.map(x => parseInt(x));//convert all the items back into numbers
}
//use the function
var myArray = numberToArray(myNumber);
Example 2: number to array javascript
const n = 123456;
Array.from(n.toString()).map(Number);
// [1, 2, 3, 4, 5, 6]