Split a number into individual digits using JavaScript
Last Updated :
12 Jul, 2025
We will get some input from the user by using <input> element and the task is to split the given number into the individual digits with the help of JavaScript. There are several approaches to splitting a number into individual digits using JavaScript which are as follows:
Using number.toString() and split('')
In this approach number.toString() converts the number to a string, split('') splits the string into an array of characters, and map(Number) converts each character back to a number. This approach effectively splits the number into its digits.
Example: To split a number into individual digits using JavaScript method.
JavaScript
function splitNumberIntoDigits(number) {
return number
.toString()
.split("")
.map(Number);
}
// Example usage
const number = 12345;
const digits = splitNumberIntoDigits(number);
console.log(digits);
Direct Character Iteration and Array Push
First take the element from input element in string format (No need to convert it to Number) and declare an empty array(var res). Visit every character of the string in a loop on the length of the string and push the character in the array(res) by using push() method.
Example: To split a number into individual digits using JavaScript method.
JavaScript
function GFG_Fun() {
let str = "123456";
let res = [];
for (
let i = 0, len = str.length;
i < len;
i += 1
) {
res.push(+str.charAt(i));
}
console.log(res);
}
GFG_Fun();
Output[ 1, 2, 3, 4, 5, 6 ]
Splitting String into Array of Characters
First take the element from input element in string format (No need to convert it to Number) and declare an empty array(var res). Split the string by using split() method on ('') and store the splitted result in the array(str).
Example: To split a number into individual digits using JavaScript method.
JavaScript
function GFG_Fun() {
let n = "123456";
let str = n.split('');
console.log(str);
}
GFG_Fun();
Output[ '1', '2', '3', '4', '5', '6' ]
Using Modulo Operator
We will be using modulo operator to get the last digit of the number and then divide the number by 10 to calculate the remaining ones and to repeat this we will use the while loop, this iteration will continue to run until the number becomes zero.
Example: To split a number into individual digits using JavaScript method.
JavaScript
function GFG_Fun() {
let number = 123456;
let digit = [];
while (number > 0) {
digit.unshift(number % 10);
number = Math.floor(number / 10);
}
console.log(digit);
}
GFG_Fun();
Output[ 1, 2, 3, 4, 5, 6 ]
Using Array.prototype.map with String Conversion
This approach utilizes the Array.prototype.map
method along with converting the number to a string to split it into individual digits.
Example: To split a number into individual digits using JavaScript method.
JavaScript
function splitDigits(number) {
return number
.toString()
.split('')
.map(digit => parseInt(digit, 10));
}
// Example usage:
const number = 987654;
const digits = splitDigits(number);
console.log(digits);
Output[ 9, 8, 7, 6, 5, 4 ]
Similar Reads
How to generate a n-digit number using JavaScript? The task is to generate an n-Digit random number with the help of JavaScript. You can also generate random numbers in the given range using JavaScript. Below are the approaches to generate a n-digit number using JavaScript: Table of Content Using Math.random()Math.random() Method and .substring() Me
2 min read
How to get decimal portion of a number using JavaScript ? Given a float number, The task is to separate the number into integer and decimal parts using JavaScript. For example, a value of 15.6 would be split into two numbers, i.e. 15 and 0.6 Here are a few methods discussed. These are the following methods: Table of Content Javascript String split() Method
3 min read
Convert a String to Number in JavaScript To convert a string to number in JavaScript, various methods such as the Number() function, parseInt(), or parseFloat() can be used. These functions allow to convert string representations of numbers into actual numerical values, enabling arithmetic operations and comparisons.Below are the approache
4 min read
Extract a Number from a String using JavaScript We will extract the numbers if they exist in a given string. We will have a string and we need to print the numbers that are present in the given string in the console.Below are the methods to extract a number from string using JavaScript:Table of ContentUsing JavaScript match method with regExUsing
4 min read
How To Split a String Into Segments Of n Characters in JavaScript? When working with JavaScript, you might encounter a situation where you need to break a string into smaller segments of equal lengths. This could be useful when formatting a serial number, creating chunks of data for processing, or splitting a long string into manageable pieces. In this article, we
6 min read
Build a Spy Number Checker using HTML CSS and JavaScript In the realm of mathematics, Spy Numbers, also known as secretive numbers or cryptic numbers, possess a unique property. A spy number is defined as a number whose sum of digits is equal to the product of its digits. In this article, we will explore how to build a Spy Number Checker using HTML, CSS,
3 min read