Sum all the Digits in a Number using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
2 min

banner

# Sum all the Digits in a Number using JavaScript

To sum all the digits in a number:

  1. Convert the number to a string.
  2. Use the split() method to split the string into an array of digits.
  3. Use the reduce() method to sum up all the digits in the array.
index.js
function getSumOfDigits(num) { return String(num) .split('') .reduce((accumulator, digit) => { return accumulator + Number(digit); }, 0); } console.log(getSumOfDigits(1)); // ๐Ÿ‘‰๏ธ 1 console.log(getSumOfDigits(123)); // ๐Ÿ‘‰๏ธ 6 console.log(getSumOfDigits(1234)); // ๐Ÿ‘‰๏ธ 10

sum all digits in number

The code for this article is available on GitHub

The function takes a number as a parameter and returns the sum of the digits of the number.

We used the String() constructor to convert the number to a string and then used the String.split() method to split the string into an array of digits.

index.js
console.log('1'.split('')); // ๐Ÿ‘‰๏ธ ['1'] console.log('123'.split('')); // ๐Ÿ‘‰๏ธ ['1', '2', '3'] console.log('1234'.split('')); // ๐Ÿ‘‰๏ธ ['1', '2', '3', '4']

We passed an empty string to the String.split() method to split the string on each character (digit).

The last step is to use the Array.reduce() method to iterate over the array.

index.js
function getSumOfDigits(num) { return String(num) .split('') .reduce((accumulator, digit) => { return accumulator + Number(digit); }, 0); } console.log(getSumOfDigits(1)); // ๐Ÿ‘‰๏ธ 1 console.log(getSumOfDigits(123)); // ๐Ÿ‘‰๏ธ 6 console.log(getSumOfDigits(1234)); // ๐Ÿ‘‰๏ธ 10
The code for this article is available on GitHub

The function we passed to the reduce() method gets called with each element (digit) in the array.

The second argument we passed to the method is the initial value for the accumulator variable.

On each iteration, we return the sum of the accumulator and the current digit.

Whatever we return from the callback function gets passed as the accumulator on the next iteration.

Once the reduce() method iterates over the entire array, we have the sum of all the digits in the number.

Alternatively, you can use a while loop.

# Sum all the Digits in a Number using a while loop

This is a three-step process:

  1. Initialize a sum variable to 0.
  2. Use the modulo operator to get each digit of the number.
  3. Add each digit to the sum variable.
index.js
function getSumOfDigits(num) { let initialNumber = num; let sum = 0; while (initialNumber) { sum += initialNumber % 10; initialNumber = Math.floor(initialNumber / 10); } return sum; } console.log(getSumOfDigits(1)); // ๐Ÿ‘‰๏ธ 1 console.log(getSumOfDigits(123)); // ๐Ÿ‘‰๏ธ 6 console.log(getSumOfDigits(1234)); // ๐Ÿ‘‰๏ธ 10

sum all digits in number using while loop

The code for this article is available on GitHub

We used a while loop to iterate for as long as the initialNumber variable stores a truthy value (not 0).

On each iteration, we use the modulo % operator to get the remainder of dividing the number by 10.

index.js
console.log(1234 % 10); // ๐Ÿ‘‰๏ธ 4 console.log(123 % 10); // ๐Ÿ‘‰๏ธ 3 console.log(12 % 10); // ๐Ÿ‘‰๏ธ 2

The expression returns the last digit of the number.

The next step is to reassign the initialNumber variable so that the last digit is removed.

On each iteration, we get the last digit of the number and add it to the sum variable.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev