0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Sum of Digits

You can find the sum of digits in JavaScript by converting the number to a string, splitting it into digits, then summing them using split(''), map(Number), and reduce((a, b) => a + b, 0) like this: const sum = num => num.toString().split('').map(Number).reduce((a, b) => a + b, 0);
📋

Examples

Input123
Output6
Input405
Output9
Input0
Output0
🧠

How to Think About It

To find the sum of digits, first think of the number as a string so you can look at each digit separately. Then, convert each character back to a number and add them all together. This way, you treat each digit like a small number and sum them up.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string to separate digits.
3
Split the string into individual characters (digits).
4
Convert each character back to a number.
5
Add all the numbers together to get the sum.
6
Return or print the sum.
💻

Code

javascript
function sumOfDigits(num) {
  return num.toString().split('').map(Number).reduce((a, b) => a + b, 0);
}

console.log(sumOfDigits(123)); // Output: 6
Output
6
🔍

Dry Run

Let's trace the number 123 through the code to find the sum of its digits.

1

Convert number to string

123 becomes '123'

2

Split string into digits

'123' becomes ['1', '2', '3']

3

Convert each digit to number

['1', '2', '3'] becomes [1, 2, 3]

4

Sum all digits

1 + 2 + 3 = 6

StepDigits ArraySum So Far
Start['1', '2', '3']0
Add 1[1, 2, 3]1
Add 2[1, 2, 3]3
Add 3[1, 2, 3]6
💡

Why This Works

Step 1: Convert number to string

Using toString() lets us treat the number like text so we can look at each digit separately.

Step 2: Split string into characters

The split('') method breaks the string into an array of single characters, each representing a digit.

Step 3: Convert characters to numbers

Using map(Number) changes each character back into a number so we can add them.

Step 4: Sum the digits

The reduce method adds all the numbers in the array to get the total sum.

🔄

Alternative Approaches

Using a loop and modulo operator
javascript
function sumOfDigits(num) {
  let sum = 0;
  while (num > 0) {
    sum += num % 10;
    num = Math.floor(num / 10);
  }
  return sum;
}

console.log(sumOfDigits(123)); // Output: 6
This method works directly with numbers without converting to strings, which can be faster for large numbers.
Using recursion
javascript
function sumOfDigits(num) {
  if (num === 0) return 0;
  return (num % 10) + sumOfDigits(Math.floor(num / 10));
}

console.log(sumOfDigits(123)); // Output: 6
This method uses a function calling itself to break down the problem, which is elegant but can be less efficient for very large numbers.

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

Time Complexity

The program processes each digit once, so the time grows linearly with the number of digits, making it O(n).

Space Complexity

Converting the number to a string and then to an array uses extra space proportional to the number of digits, so O(n).

Which Approach is Fastest?

The loop with modulo method uses constant space O(1) and is generally faster for large numbers compared to string conversion.

ApproachTimeSpaceBest For
String conversion with map and reduceO(n)O(n)Simplicity and readability
Loop with modulo operatorO(n)O(1)Performance with large numbers
RecursionO(n)O(n)Elegant code but less efficient for big inputs
💡
Convert the number to a string to easily access each digit for summing.
⚠️
Forgetting to convert string digits back to numbers before adding, which causes string concatenation instead of numeric addition.