

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Write a number array and using for loop add only even numbers in javascript?
- Enter a number and write a function that adds the digits together on button click in JavaScript
- How to find all unique triplets that adds up to sum Zero using C#?
- How to write a for loop in a JSP page?
- How do I create dynamic variable names inside a JavaScript loop?
- 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
- Can re-declaring a variable destroy the value of that variable in JavaScript?
- How can I write a try/except block that catches all Python exceptions?
- How to use for loop to print all the elements of a list in R?
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- Write a C Program to delete the duplicate numbers in an array
- Removing the second number of the pair that add up to a target in JavaScript
- How to write a while loop in a JSP page?
Advertisements