To sum the digits of a number in JavaScript, we can use a for loop which will get each digit from the number and add them together. Here is a quick function that will take a number and then sum it’s digits:

```
function sumDigits(num){
var sum = 0;
var numString = num + "";
for ( var i = 0; i < numString.length; i++ ){
sum = sum + Number(numString.charAt(i));
};
return sum;
}
```

Let’s break down this function line by line.

In our sumDigits() function, we first start out by creating a variable that we will use to help sum up all the numbers, called **sum**.

We next convert the number into a string.

We then use a **for loop** to loop through all of the digits in the number. In this for loop, we use the charAt() method to help iterate over each digit in the number. We then add that number to our overall sum.

Finally, we return the value of the sum of the digits of the number.

Let’s see this function in action below.

## Sum the Digits of a Number in JavaScript With a Click

To sum up the digits of a number on click using JavaScript, we can combine the function **sumDigits()** we created above with a click event.

Below we will provide code to let the user input a number, and then use our function to return the results. Here is our simple HTML setup:

```
<p>Type a number you want to sum the digits of:</p>
<input id="userVal" type="text" value="" onkeyup="sumDigits(event)">
<input id="submitNum" type="submit" value="Submit" onclick="sumDigits('click')">
<div id="results"></div>
```

Below is the JavaScript code which take the user input using the onkeyup event or the **onclick event** along with the value property and use our function **sumDigits()** on that user input and update the results below using the textContent property.

Here is the JavaScript code:

```
function sumDigits(e) {
//If we clicked or pressed enter, run the following code
if( e == 'click' || e.keyCode == 13 ){
//Get the number the user entered
var userNum = document.getElementById("userVal").value;
//Use code from our function above
var sum = 0;
var numString = userNum + "";
for ( var i = 0; i < numString.length; i++ ){
sum = sum + Number(numString.charAt(i));
};
//Show the result to the user to the user
document.getElementById("results").textContent = sum;
}
};
```

The final code and output for this example are below:

Code Output:

Type a number you want to sum the digits of:

Full Code:

```
<p>Type a number you want to sum the digits of:</p>
<input id="userVal" type="text" value="" onkeyup="sumDigits(event)">
<input id="submitNum" type="submit" value="Submit" onclick="sumDigits('click')">
<div id="results"></div>
<script>
function sumDigits(e) {
if( e == 'click' || e.keyCode == 13 ){
var userNum = document.getElementById("userVal").value;
var sum = 0;
var numString = userNum + "";
for ( var i = 0; i < numString.length; i++ ){
sum = sum + Number(numString.charAt(i));
};
document.getElementById("results").textContent = sum;
}
};
</script>
```

Hopefully this article has been useful in helping you understand how to sum up the digits of a number in JavaScript.

## Leave a Reply