sum = 0;
while (value) {
sum += value % 10;
value = Math.floor(value / 10);
}
const sumDigits = num => num.toString().split('').map(Number).reduce((a,b) => a + b);
//usage: mySum=sumDigits(myNum); console.log(sumDigits(1234)) output: 10
<script>
// Javascript program to compute sum of digits in
// number.
/* Function to get sum of digits */
function getSum(n)
{
var sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = parseInt(n / 10);
}
return sum;
}
// Driver code
var n = 687;
document.write(getSum(n));
</script>
// Sum of all the digits of a number in Javascript
const num = 1245,
sum = num
.toString()
.split('')
.map(Number)
.reduce((a,b) => a + b);
console.log(sum); // 12