Skip to content

Instantly share code, notes, and snippets.

@jfbrennan
Last active March 28, 2020 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfbrennan/8de3ec9814405dd0440a42d067169acb to your computer and use it in GitHub Desktop.
Save jfbrennan/8de3ec9814405dd0440a42d067169acb to your computer and use it in GitHub Desktop.
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