0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Sum of Digits Using Recursion

You can find the sum of digits of a number using recursion in JavaScript by defining a function like function sumDigits(n) { if (n === 0) return 0; return n % 10 + sumDigits(Math.floor(n / 10)); } which adds the last digit to the sum of the remaining digits recursively.
📋

Examples

Input123
Output6
Input0
Output0
Input9999
Output36
🧠

How to Think About It

To find the sum of digits using recursion, think of the number as made of its last digit plus the rest. Use n % 10 to get the last digit and Math.floor(n / 10) to remove it. Keep adding the last digit to the sum of the remaining digits until the number becomes zero.
📐

Algorithm

1
Get the input number.
2
Check if the number is zero; if yes, return zero as the sum.
3
Find the last digit using modulo 10.
4
Call the function recursively with the number divided by 10 (without the last digit).
5
Add the last digit to the result of the recursive call.
6
Return the total sum.
💻

Code

javascript
function sumDigits(n) {
  if (n === 0) return 0;
  return n % 10 + sumDigits(Math.floor(n / 10));
}

console.log(sumDigits(123));
Output
6
🔍

Dry Run

Let's trace sumDigits(123) through the code

1

Initial call

n = 123, last digit = 3, call sumDigits(12)

2

Second call

n = 12, last digit = 2, call sumDigits(1)

3

Third call

n = 1, last digit = 1, call sumDigits(0)

4

Base case

n = 0, return 0

5

Return from third call

1 + 0 = 1

6

Return from second call

2 + 1 = 3

7

Return from initial call

3 + 3 = 6

CallnLast DigitRecursive CallReturn Value
sumDigits(123)1233sumDigits(12)6
sumDigits(12)122sumDigits(1)3
sumDigits(1)11sumDigits(0)1
sumDigits(0)0--0
💡

Why This Works

Step 1: Base case stops recursion

When n becomes zero, the function returns 0 to stop further recursive calls.

Step 2: Extract last digit

Using n % 10 gets the last digit of the number to add to the sum.

Step 3: Recursive call reduces problem size

Calling the function with Math.floor(n / 10) removes the last digit, making the problem smaller each time.

🔄

Alternative Approaches

Using string conversion and recursion
javascript
function sumDigitsStr(n) {
  if (n.length === 0) return 0;
  return Number(n[0]) + sumDigitsStr(n.slice(1));
}

console.log(sumDigitsStr('123'));
This method converts the number to a string and sums digits recursively, but uses more memory due to string operations.
Using iteration instead of recursion
javascript
function sumDigitsIter(n) {
  let sum = 0;
  while (n > 0) {
    sum += n % 10;
    n = Math.floor(n / 10);
  }
  return sum;
}

console.log(sumDigitsIter(123));
This iterative approach avoids recursion and is more efficient for very large numbers.

Complexity: O(d) time, O(d) space

Time Complexity

The function calls itself once per digit, so time grows linearly with the number of digits d.

Space Complexity

Each recursive call adds a layer to the call stack, so space used is proportional to the number of digits.

Which Approach is Fastest?

The iterative approach is faster and uses less memory, but recursion is simpler and clearer for learning.

ApproachTimeSpaceBest For
RecursionO(d)O(d)Learning recursion and clarity
String conversion recursionO(d)O(d)When working with digit strings
IterationO(d)O(1)Performance and large inputs
💡
Always include a base case in recursion to avoid infinite calls.
⚠️
Forgetting to use Math.floor when dividing by 10 causes infinite recursion with decimal numbers.