Skip to content

Instantly share code, notes, and snippets.

@jfbrennan
Last active Mar 28, 2020
Embed
What would you like to do?
JavaScript convert number to array of digits
const num = 123
let digits
// Option A
digits = num.toString().split('').map(Number)
console.log(digits) // [1, 2, 3]
// Option B
digits = Array.from(String(num), Number)
console.log(digits) // [1, 2, 3]
// TODO are there *any* differences?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment