JavaScript - Convert a Number into JS Array
Last Updated :
10 Jan, 2025
You have a number, like 12345, and you need to convert it into an array where each element represents a digit of the number. For example, 12345 should become [1, 2, 3, 4, 5]. How can you achieve this in JavaScript?
In JavaScript, there are various ways to transform a number into an array of its digits. This can be useful in scenarios like performing calculations on each digit, validating number patterns, or just reformatting data for better usability.

These are the following methods by using these we can solve this problem:
1. Using Array.from() Method
The JavaScript Array.from() method creates an array from an object with a length property or an iterable. To use it, store a number in a variable and typecast it to a string as the first parameter of Array.from(). The second parameter is a callback function (e.g., myFunc), called for each character in the string. Inside the callback, convert the character back to an integer and return it. The resulting array contains the individual digits of the number.
JavaScript
let num = 235345;
// Getting the string as a parameter
// and typecasting it into an integer
let Func = num => Number(num);
let arr = Array.from(String(num), Func);
// Print the result array
console.log(arr);
Output[ 2, 3, 5, 3, 4, 5 ]
2. Using mathematical operations
In this approach we use a while loop to iterate over the digit of a number. In each iteration of the loop, we use the modulo operator (%) to get the last digit of the number and add it to the beginning of the array using unshift(). We then update the number by dividing it by 10 and taking the floor value using Math.floor(). This process repeats until the number becomes 0.
JavaScript
let num = 12345;
let arr = [];
while (num > 0) {
arr.unshift(num % 10);
num = Math.floor(num / 10);
}
console.log(arr);
3. Using a For Loop
The for loop approach involves converting the number to a string, iterating through each character of the string, and converting each character back to a number. These numbers are then pushed into an array, resulting in an array of the number's digits.
JavaScript
const num = 12345;
const arr = [];
const str = String(num);
for (let i = 0; i < str.length; i++) {
arr.push(Number(str[i]));
}
console.log(arr);
4. Using String.prototype.split() Method
Another approach to convert a number into an array in JavaScript is by using the split() method directly on the string representation of the number. This method involves converting the number to a string, splitting the string into an array of characters, and then converting each character back into a number.
JavaScript
const number = 235345;
let res = number.toString().split('').map(Number);
console.log(res);
Output[ 2, 3, 5, 3, 4, 5 ]
5. Using Recursion
This approach uses recursion to break down the number into its individual digits and constructs the array step-by-step. The function calls itself with the remaining digits of the number until the number is reduced to zero.
JavaScript
function fun(num) {
if (num === 0) {
return [];
}
const dig = fun(Math.floor(num / 10));
dig.push(num % 10);
return dig;
}
const num = 235345;
const res = fun(num);
console.log(res);
Output[ 2, 3, 5, 3, 4, 5 ]
Similar Reads
How numbers are stored in JavaScript ? In this article, we will try to understand how numbers are stored in JavaScript. Like any other programming language, all the data is stored inside the computer in the form of binary numbers 0 and 1. Since computers can only understand and process data in the form of 0's and 1's. In JavaScript, ther
6 min read
How to create a Number object using JavaScript ? In this article, we will discuss how to create a Number object using JavaScript. A number object is used to represent integers, decimal or float point numbers, and many more. The primitive wrapper object Number is used to represent and handle numbers. examples: 20, 0.25. We generally don't need to w
2 min read
How to convert a value to a safe integer using JavaScript ? In this article, we will learn How to convert a value to a safe integer using JavaScript. We have given a number, and we have to convert it into a safe integer using JavaScript. Safe Integers are integers between -(253 - 1) and (253 - 1). Approach: First, we take input using the prompt in JavaScript
2 min read
How to add float numbers using JavaScript ? Given two or more numbers the task is to get the float addition in the desired format with the help of JavaScript. There are two methods to solve this problem which are discussed below: Table of Content Using parseFloat() and toFixed() method Using parseFloat() and Math.round() method Using Number()
2 min read
Calculate current week number in JavaScript Calculating the current week number involves determining which week of the year the current date falls into. The method of calculation can vary slightly depending on the rules you follow, such as which day starts the week (Sunday or Monday) and how the first week of the year is defined.For example:J
2 min read
How to Convert a Float Number to the Whole Number in JavaScript? Given a float number and the task is to convert a float number to a whole number using JavaScript. Below are various methods to convert float numbers to whole numbers in JavaScript:Table of ContentMath.floor (floating argument)Math.ceil (floating argument) Math.round (floating argument)Math.trunc (f
4 min read
How to convert Number to Boolean in JavaScript ? We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer â0â or â1â into Boolean Value i.e. "false" or "true". Below are
2 min read
Convert a Number to a String in JavaScript These are the following ways to Convert a number to a string in JavaScript:1. Using toString() Method (Efficient and Simple Method)This method belongs to the Number.Prototype object. It takes an integer or a floating-point number and converts it into a string type.JavaScriptlet a = 20; console.log(a
1 min read
Convert a negative number to positive in JavaScript In this article, we will see how we can convert a negative number to a positive number in JavaScript by the methods described below. Below are the methods to convert a negative number to a positive in JavaScript: Table of Content Multiplying by -1Using Math.abs()adding a minus signFlipping the bitUs
4 min read
Check a Number is Prime or Not Using JavaScript A prime number is a whole number greater than 1, which has no positive divisors other than 1 and itself. In other words, prime numbers cannot be formed by multiplying two smaller natural numbers. For example:2, 3, 5, 7, 11, and 13 are prime numbers.4, 6, 8, 9, and 12 are not prime numbers because th
5 min read