Open In App

JavaScript Program for Sum of Digits of a Number

Last Updated : 28 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to learn about finding the Sum of Digits of a number using JavaScript. The Sum of Digits refers to the result obtained by adding up all the individual numerical digits within a given integer. It’s a basic arithmetic operation. This process is repeated until a single-digit sum, also known as the digital root.

There are several methods that can be used to find the Sum Of Digits by using JavaScript, which are listed below:


We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using Array Reduce() Method

In this approach, the reduce method transforms each digit of a number into an accumulated sum. It converts the number to a string, iterates, and adds each digit to the sum.

Syntax:

array.reduce( function(total, currentValue, currentIndex, arr), initialValue )

Example: In this example, the sumOfDigit function converts the number to a string, splits it into digits, and then reduces by adding parsed digits, resulting in the sum.

JavaScript
function sumOfDigit(num) {
    return num.toString().split("")
        .reduce((sum, digit) =>
            sum + parseInt(digit), 0);
}

console.log(sumOfDigit(738));

Output
18

Approach 2: Using For…of Loop

In this approach, Iterate through each digit of a number by converting it to a string, then use a for…of loop to add parsed digits, resulting in the sum.

Syntax:

for ( variable of iterableObjectName) {
. . .
}

Example: In this example we are using the above-explained approach.

JavaScript
function sumOfDigit(num) {
    let numStr = num.toString();
    let sum = 0;

    for (let digit of numStr) {
        sum += parseInt(digit);
    }

    return sum;
}

console.log(sumOfDigit(738)); 

Output
18

Approach 3: Using Math.floor and Division

In this approach, we calculate sum by repeatedly adding last digit using remainder of 10 and updating number by division, until it’s 0.

Syntax:

Math.floor( value )

Example: In this example we are using above-explained approach.

JavaScript
function sumOfDigits(num) {
    let sum = 0;
    for (; num > 0; num = Math.floor(num / 10)) {
        sum += num % 10;
    }
    return sum;
}

console.log(sumOfDigits(456));

Output
15

Approach 4: Using forEach

In this approach, we are converting the number to a string, split it into digits, and use forEach loop to add parsed digits, obtaining the sum.

Syntax:

array.forEach(callback(element, index, arr), thisValue)

Example: In this example, the number 123 is converted to a string and then split into an array of individual digits. The forEach loop iterates through each digit in the array. Inside the loop, each digit is parsed and added to the sum variable.

JavaScript
function sumOfDigit(num) {
    let sum = 0;
    num.toString().split("").forEach(digit => {
        sum += parseInt(digit);
    });
    return sum;
}

console.log(sumOfDigit(123));

Output
6

Approach 5: Using Recursion

In this approach, we use a recursive function to repeatedly add the last digit of the number (obtained using modulo 10) and call itself with the number divided by 10 until the number becomes 0.

Example: In this example, the recursiveSum function calls itself with the number divided by 10, adding the last digit each time, until the number becomes 0.

JavaScript
function recursiveSum(num) {
    if (num === 0) {
        return 0;
    }
    return (num % 10) + recursiveSum(Math.floor(num / 10));
}

console.log(recursiveSum(738));

Output
18




Next Article

Similar Reads