• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Get the Sum of Array in JavaScript

Get the Sum of Array in JavaScript

September 16, 2022 Leave a Comment

To get the sum of an array in JavaScript, we can use the JavaScript Array reduce() method. Here is the setup for how this can be done:

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function sumArray(total, item) {
  return total + item;
}

console.log(numbers.reduce(sumArray));

#Output
45

We can shorten our code even more by using the JavaScript arrow function.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var sumOfNumbers = numbers.reduce((a, b) => a + b);

console.log(sumOfNumbers);

#Output
45

We can also use a for loop to sum the numbers of an array easily.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var total = 0;
for( var i =0; i<numbers.length; i++ ){
  total += numbers[i];
}

console.log(total);

#Output
45

When working with collections of data in JavaScript, the ability to summarize the data easily is valuable.

One such case is if you want to get the sum of an array of numbers.

To calculate the sum of an array of numbers in JavaScript, the easiest way is with the reduce() method.

Below again we show you how to get the sum of an array of numbers in JavaScript.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function sumArray(total, item) {
  return total + item;
}

console.log(numbers.reduce(sumArray));

#Output
45

Using a For Loop to Get the Sum of an Array in JavaScript

Another way you can add numbers of an array together is with a for loop.

To add the numbers of an array up, initialize a variable that will keep the running sum and then add each element to the running sum.

Below shows you how to get the sum of an array of numbers in JavaScript with a for loop.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var total = 0;
for( var i =0; i<numbers.length; i++ ){
  total += numbers[i];
}

console.log(total);

#Output
45

We can put our code in a function to reuse the function to sum an array.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function sumArray(arr){
  var total = 0;
  for( var i =0; i<arr.length; i++ ){
    total += arr[i];
  }
  return total;
};

console.log(sumArray(numbers));

#Output
45

Hopefully this article has been useful for you to learn how to get the sum of an array in JavaScript.

Other Articles You'll Also Like:

  • 1.  How to Create a New h1 Element With JavaScript
  • 2.  Reverse Words in a String JavaScript
  • 3.  Using JavaScript to Resize an Image
  • 4.  JavaScript isPrime – How to Check if a Number is Prime in JavaScript
  • 5.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 6.  JavaScript Trunc with Math.trunc() Method
  • 7.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 8.  Using Javascript to Add Class to Element
  • 9.  Creating a JavaScript Function to Subtract Two Numbers
  • 10.  How to Create a JavaScript Count Up Timer

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy