Open In App

Split a number into individual digits using JavaScript

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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);

Output
[ 1, 2, 3, 4, 5 ]

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 ]


Next Article

Similar Reads