- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How could I write a for loop that adds up all the numbers in the array to a variable in JavaScript?
At first, declare and initialize the total variable with 0 and then you need to iterate over all the array and extract all the values of array and add up with the updated total variable.
Let’s say the following is our array with numbers −
var listOfValues = [10,3,4,90,34,56,23,100,200];
Example
var listOfValues = [10,3,4,90,34,56,23,100,200]; var total = 0; for(let index = 0; index < listOfValues.length ; index++){ total=total+listOfValues[index]; } console.log("Total Values="+total);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo86.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo86.js Total Values=520
- Related Articles
- Write a number array and using for loop add only even numbers in javascript?
- How to find all unique triplets that adds up to sum Zero using C#?
- How do I create dynamic variable names inside a JavaScript loop?
- Enter a number and write a function that adds the digits together on button click in JavaScript
- How to write a for loop in a JSP page?
- How can I write in order with for loop or while loop?
- Find the longest sub array of consecutive numbers with a while loop in JavaScript
- Function to add up all the natural numbers from 1 to num in JavaScript
- How to use for loop to print all the elements of a list in R?
- How to use for...in statement to loop through an Array in JavaScript?
- How to show for loop using a flowchart in JavaScript?
- How to loop through all the elements of an array in C#?
- How to show a nested for loop in a flow chart in JavaScript?
- How to find all elements in a given array except for the first one using JavaScript?
- Why is using “for…in” loop in JavaScript array iteration a bad idea?

Advertisements