In this tutorial, you’ll learn how to split numbers to individual digits using JavaScript. JavaScript split method can be used to split number into array. But split method only splits string, so first you need to convert Number to String. Then you can use the split method with a combination of map method to transform each letter to Number.

Let’s have a look at how to achieve it.

function splitToDigit(n){
  return (n + '').split('').map((i) => { return Number(i); })
}

console.log(splitToDigit(100))

As seen in the above code, n + ‘ ‘ converts the Number to string. Then the String is converted to an array using split method. The each string letter is converted to Number.

You can reduce the above code further using the spread operator. You can simple use map by passing in the map callback function.

function splitToDigit(n){
  return [...n + ''].map(Number)
}

console.log(splitToDigit(100))