Languages
[Edit]
EN

JavaScript - convert integer number to array of digits

0 points
Created by:
Zayaan-Rasmussen
543

In this article, we would like to show you how to convert integer number to array of digits in JavaScript.

Practical examples

Edit

Solution 1

Edit

In this section, we convert the integer number to array of digits using three methods:

  1. toString() - converts the number to a string,
  2. from() - creates an array of strings from previously created string,
  3. map() - maps all the elements from an array of strings to Number type.

Solution 2

Edit

In this solution, we create a reusable arrow function that iterates through the number dividing it by 10 in each step.

Solution 3 - the most efficient

Edit

In this solution we use log10 to calculate the length of a number, then we use this to allocate an array, which makes the approach more efficient.

 

References

Edit
  1. Array.from() - JavaScript | MDN
  2. Number.prototype.toString() - JavaScript | MDN
  3. Array.prototype.map() - JavaScript | MDN
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join