var nums = ['100','300','400','60','40'];
var sum = 0;

for(var i=0; i < nums.length; i++){

    sum += parseInt(nums[i]);

}

alert(sum);

Tested: http://jsfiddle.net/GYpd2/6/ (thanks to user1503606)

If nums contains numbers only there is no need for parseInt().

Answer from Besnik on Stack Overflow
🌐
Reddit
reddit.com › r/learnjavascript › how to correctly sum up this loop and return properly?
r/learnjavascript on Reddit: how to correctly sum up this loop and return properly?
December 13, 2022 -

I can't figure why my sum in this loop doesn't add up like it should.

const sumAll = function(a, b) {
  for (let i = a; i < b; i++) {
    let sum = 0
    sum += i
    console.log(sum)
    // return sum
  }
};

console.log(sumAll(1, 4))

every iteration i become one higher integer and in this case 1 to 4, but looking at the console.log every iteration i keep getting addition of only 1?Also i'm not sure how to properly return in a loop because it returns after 1 loop, i tried using if ( i === b ) then return sum but it won't recoginize it from the if statement, the same is if you use return sum between the last 2 curly brackets.

the output of sumAll should be 1+2+3+4=10

If you got any tips i would love to hear them!

Discussions

html - JavaScript, Using a for loop to calculate the sum of all the values within an array - Stack Overflow
I just starting using JavaScript a couple weeks ago and still struggling a bit. I need to create a loop that calculates the sum of all values coming from a votes array in a separate .js file. The More on stackoverflow.com
🌐 stackoverflow.com
July 12, 2017
With Javascript use a for loop to sum numbers in an array - Stack Overflow
I'm trying to find a way to sum all the numbers that have been added to an array. I believe this should work: var total = 0; for (var i = 0; i More on stackoverflow.com
🌐 stackoverflow.com
April 17, 2017
javascript - Loop through array and return sum of all values - Stack Overflow
What I want to do is have numbers inputted by user and the sum of the numbers returned. My logic is as follows: User inputs string String is split to array Loop through array and sum all numbers R... More on stackoverflow.com
🌐 stackoverflow.com
How do I write a function that should return true if ANY pair of numbers in an array add up to a target number? I’ve only figured it out for the first and last so far.
You need to loop through your array, store each number that you've seen and check if the result of target number minus the number you are currently looking at is a number you have already seen. If this is ever true, return true. At the end you can safely return false because you will only reach it if no numbers equal the target. More on reddit.com
🌐 r/learnjavascript
75
86
July 15, 2022
🌐
DoFactory
dofactory.com › javascript › loops
JavaScript Loops
var sum = 0; var number = 1; do { sum += number; // -- body number++; // -- updater } while (number <= 50); // -- condition console.log("Sum = " + sum); // => Sum = 1275 ... The block following do is executed first and then the condition is evaluated. If the while condition is true, the block ...
🌐
Programiz
programiz.com › javascript › examples › sum-natural-number
JavaScript Program to Find the Sum of Natural Numbers
// program to display the sum of natural numbers // take input from the user const number = parseInt(prompt('Enter a positive integer: ')); let sum = 0; // looping from i = 1 to number // in each iteration, i is increased by 1 for (let i = 1; i <= number; i++) { sum += i; } console.log('The ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions › recursion and stack
Sum all numbers till the given one
function sumTo(n) { let sum = 0; for (let i = 1; i <= n; i++) { sum += i; } return sum; } alert( sumTo(100) ); ... P.S. Naturally, the formula is the fastest solution. It uses only 3 operations for any number n. The math helps!
🌐
HTML Academy
htmlacademy.org › courses › javascript-programming › arrays › sum-in-loop
Summation in the loop — Arrays — HTML Academy
To do this, you must enter a variable with a zero value before the loop. Then at each iteration add to it the value of the next element of the array. As a result, after the loop, the variable will be the sum of all the elements.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-conditional-statements-and-loops-exercise-12.php
JavaScript conditional statement and loops: Sum the multiples of 3 and 5 under 1000 - w3resource
July 10, 2025 - ... // Variable to store the sum of numbers divisible by 3 or 5 var sum = 0; // Loop through numbers from 0 to 999 for (var x = 0; x < 1000; x++) { // Check if the current number is divisible by 3 or 5 if (x % 3 === 0 || x % 5 === 0) { // Add ...
Find elsewhere
🌐
DigiFisk
digifisk.com › home › tutorials › how to find the sum of numbers using a for loop in javascript?
How to find the sum of numbers using a for loop in JavaScript? - DigiFisk
May 24, 2022 - We’re going to use the ‘+=’ to add the new value of ‘i’ to the old value of ‘sum’. Now you understand why we declared ‘sum’ with a value of 0. We did that so we have a number, albeit one with no value, that can be added to the first value of ‘i’. ... Once the for loop is done, we can print it to our page by changing the innerText of the ‘output’ variable.
🌐
Code.mu
code.mu › en › javascript › book › prime › loops › numbers-accumulation
Accumulation of numbers in loops in JavaScript | Trepachev Dmitry
In this lesson, we will learn how to accumulate the sum of numbers into a variable inside the loop in JavaScript.
🌐
OneCompiler
onecompiler.com › javascript › 3xcm4hb8n
Sum of Natural Numbers Using for Loop - JavaScript - OneCompiler
// program to display the sum of natural numbers const number = 100; let sum = 0; for(i = 0; i <= number; i++){ sum = sum + i; } console.log(`sum of first ${number} natural numbers is: `, sum); ... Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free.
🌐
Home and Learn
homeandlearn.co.uk › javascript › javascript_for_loops.html
Javascript programming loops - for loops
This is the whole point of having a loop - to run some code over and over again. We want to add up the numbers 1 to 10. So, again, do the calculation to the right of the equal sign first. Javascript will look at whatever value is currently held in the answer variable.
🌐
Sololearn
sololearn.com › en › Discuss › 2418526 › how-to-print-sum-of-a-user-input-in-javascript-loops
How to print sum of a user input in JavaScript loops? | Sololearn: Learn to code for FREE!
Hi I want something like this var one= prompt("Please enter a number"); var two= prompt("Please enter a number"); var sum = one+ two for(i = 1; i<= sum; i++){ document.write(sum) } in this case, I want to get two inputs from the user and sum these numbers together Examples: the user enters 5 in the first input the user enters 5 in the second input I want the Result of Sum of them to be in a for loops or while loops I don't know which one is better the result on the screen should be like this 1 2 3 4 5 6 7 8 9 10 as you see we have two inputs (5 and 5) and sum of both inputs should be a variable in for loop if i is < or = sum you should add one number to i variable I hope I've explained my goal · javascriptinputloopswhilefunctionforvariableloopinputs
🌐
Vultr Docs
docs.vultr.com › javascript › examples › find-the-sum-of-natural-numbers
JavaScript Program to Find the Sum of Natural Numbers | Vultr Docs
November 11, 2024 - In this code, sumNaturalNumbers is a function that computes the sum of all natural numbers up to n. The loop iteratively adds each number from 1 to n to sum, accumulating the total sum by the end of the loop.
🌐
Codingbeautydev
codingbeautydev.com › blog › javascript-get-sum-of-array
How to Get the Sum of an Array in JavaScript - Coding Beauty
March 18, 2023 - Overall, the for loop method was the most efficient for summing an array in JavaScript, while the forEach() method was the least efficient.
🌐
Sololearn
sololearn.com › en › Discuss › 1325502 › loop-to-add-numbers-in-array-js
Loop to add numbers in array JS | Sololearn: Learn to code for FREE!
const arr = [1,2,3,4,34,24]; var sum = 0; let i =sum; for(i=0; i < arr.length; i++ ){ sum += arr[i] } console.log(sum);