In this article, I will use some built-in methods in JavaScript to sum all the digits in a number in JavaScript. Stay tuned for the next part of the article to see how I will use them.
Sum all the digits in a number in JavaScript
Sum all the digits by For loop
Before sum all the digits by For loop, we have to get the digits of the given number and save it with the toString() and split() method.
toString() method
Syntax:
string.toString();
This method is used to convert numbers to string.
split() method
Syntax:
string.split(separator, maxsplit)
Parameters:
- separator: is the specified string to split the string. If not passed, the default will be a space.
- maxsplit: is the number of string separators, if -1 means all instances of string separator appear.
Example:
// Create an array to store the digits of Number
let numArr = num.toString().split("");
After getting the digits, we sum all the digits by For loop.
Example:
function sumAllDigitsNumber(num){
let sum = 0;
// Create an array to store the digits of Number
let numArr = num.toString().split("");
for (let i = 0; i< numArr.length; i++)
{
sum += Number(numArr[i])
}
return(sum);
}
console.log(sumAllDigitsNumber(2022));
Output:
6
In the above example, we see that I go through each element of the array that I have obtained via the toString() and split() methods and then add them together to get the sum of the digits.
Sum all the digits by available methods
Similar to the sum of all the digits by For loop method, with this method, we also have to get the digits of the number from which we can calculate their sum.
Example:
// Create an array to store the digits of Number
let numArr = num.toString().split("");
I use the same command to get the array containing the digits of the number. We will get their sum from that array through the map() and reduce() method.
map() method
Syntax:
array.map(function(currentValue, index, arr), thisValue)
Parameters:
- function: is the function that executes for each element of the array that map calls.
- currentValue: is the value of the element in the array being called by the map.
- index: is the address of the element in the array being called by the map.
- arr: is the array of the element in the array being called by map.
- thisValue: is the value passed to the default function that is undefined.
Example:
let nums = [10,3,5];
console.log(nums);
// Sum of nums
console.log(nums.map(function(current,index){ return index + ": value*10 = " + current*10}))
reduce() method
Syntax:
array.reduce(function(total,currentValue, currentIndex, arr), initialValue)
Parameters:
- function: A function runs for each element in the array being called by reduce.
- total: The return value of the function or the first value of the function.
- currentValue: The value of the element in the array being called by reducing.
- currentIndex: The address of the element in the array being called by reduce.
- arr: Is the array of the element in the array being called by reducing.
- initialValue: The value passed to be the initial value of the function.
Example:
let nums = [10,3,5];
console.log(nums);
// Sum of nums
console.log("Sum: " + nums.reduce(function(total, currentValue){return total + currentValue}))
To learn more about these two methods, visit the W3C docs.
Continuing with this article, we implement the sum of all the digits by the methods available here, which map() and reduce() methods are as follows:
function sumAllDigitsNumber(num){
// Create an array to store the digits of a number
let numArr = num.toString().split("");
let sum = numArr.map(Number).reduce(function (a, b) {return a + b;}, 0);
return sum;
}
console.log(sumAllDigitsNumber(2022));
Output:
6
In this example, I used the map() method to cast an array of digits from string to number, and then we used the reduce() method to calculate their sum, and now we have a number that is the sum of the digits of the given number.
Summary
Above are some ways to sum all the digits in a number in JavaScript. Try to follow along while reading this article. It will help you remember longer. I hope this article helps you and your program. Good luck.
Maybe you are interested:
- Get the Decimal Part of a Number in JavaScript
- How to add Leading Zeros to a Number in JavaScript
- How To Get The Sum Of An Array Of Numbers In JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css