How to find the sum of an array of numbers (2024)

The ProblemJump To Solution

You want to find the sum of an array of numbers. How do you do this with JavaScript?

The Solution

There are various ways to find the sum of an array of numbers in JavaScript. We’ll look at four common ways.

Using a for loop

The most performant method is to use a for loop:

Click to Copy

const arr = [23, 34, 77, 99, 324];let sum = 0;for (let i = 0; i < arr.length; i++) { sum += arr[i];}console.log(sum);

The sum of the array of numbers is calculated by looping through the array and adding the value of each array element to a variable called sum.

Using reduce()

Another option is to use the reduce() array method:

Click to Copy

const arr = [23, 34, 77, 99, 324];const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);console.log(sum);

The reduce() method calculates the sum of the array of numbers by executing the “reducer” callback function on each element of the array. The accumulator argument is the value of the previous call of the function. Its initial value is 0; the currentValue is the value of the array element. For each call of the function, the value of the previous function call is added to the value of the array element. The return value of the reduce() method is the final value of the “reducer” callback function after it’s been executed on each element of the array. In this case, it calculates the sum of the array numbers.

Using forEach()

The forEach() array method executes the callback function argument for each element of the array:

Click to Copy

const arr = [23, 34, 77, 99, 324];let sum = 0;arr.forEach((el) => sum += el);console.log(sum);

It uses a sum variable, like the for loop method, to calculate the sum of the array numbers.

Using a for...of loop

A for…of loop can also be used to iterate through the array items and calculate the sum of the array of numbers:

Click to Copy

const arr = [23, 34, 77, 99, 324];let sum = 0;for (const el of arr) { sum += el;}console.log(sum);

How to find the sum of an array of numbers (2024)
Top Articles
A Guide to RFP Evaluation Criteria: Basics, Tips and Examples
What Is a Good Monthly Retirement Income for a Couple?
San Angelo, Texas: eine Oase für Kunstliebhaber
Top 11 Best Bloxburg House Ideas in Roblox - NeuralGamer
Brgeneral Patient Portal
Craigslist/Phx
Craigslist Labor Gigs Albuquerque
Gwdonate Org
Erskine Plus Portal
979-200-6466
Paradise leaked: An analysis of offshore data leaks
Define Percosivism
Chastity Brainwash
Convert 2024.33 Usd
Dirt Removal in Burnet, TX ~ Instant Upfront Pricing
Where Is The Nearest Popeyes
18889183540
Ac-15 Gungeon
Apartments / Housing For Rent near Lake Placid, FL - craigslist
480-467-2273
Lacey Costco Gas Price
Maine Racer Swap And Sell
Buhl Park Summer Concert Series 2023 Schedule
Craigslist Comes Clean: No More 'Adult Services,' Ever
Japanese Emoticons Stars
Our Leadership
Kacey King Ranch
Devotion Showtimes Near The Grand 16 - Pier Park
Japanese Pokémon Cards vs English Pokémon Cards
Chattanooga Booking Report
Chris Provost Daughter Addie
Staar English 1 April 2022 Answer Key
Etowah County Sheriff Dept
Aliciabibs
Stafford Rotoworld
Hindilinks4U Bollywood Action Movies
Chatropolis Call Me
Electric Toothbrush Feature Crossword
Craigslist Binghamton Cars And Trucks By Owner
Lyons Hr Prism Login
Xre 00251
Beds From Rent-A-Center
Phone Store On 91St Brown Deer
The Pretty Kitty Tanglewood
Kenwood M-918DAB-H Heim-Audio-Mikrosystem DAB, DAB+, FM 10 W Bluetooth von expert Technomarkt
Slug Menace Rs3
Zits Comic Arcamax
Image Mate Orange County
Hy-Vee, Inc. hiring Market Grille Express Assistant Department Manager in New Hope, MN | LinkedIn
Leslie's Pool Supply Redding California
Yoshidakins
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 6122

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.