How to Compute the Sum and Average of Elements in an Array in JavaScript? Last Updated : 02 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Computing the sum and average of elements in an array involves iterating through the array, accumulating the sum of its elements, and then dividing the sum by the total number of elements to get the average. This can be achieved using various techniques, from basic loops to more advanced functional methods provided by JavaScript.These are the following approaches to computing the sum and average of elements in an array:Table of ContentUsing a for LoopUsing reduce() MethodUsing forEach() MethodUsing map() and reduce() MethodsUsing a for LoopThe simplest way to compute the sum and average is by using a basic for loop to iterate through the array, adding up each element's value to a sum variable, and then dividing by the length of the array to find the average.Example: This example shows the use of for loop to get the sum and average of an array. JavaScript const array = [10, 20, 30, 40, 50]; let sum = 0; for (let i = 0; i < array.length; i++) { sum += array[i]; // Add each element to sum } let average = sum / array.length; console.log("Sum:", sum); console.log("Average:", average); OutputSum: 150 Average: 30 Using reduce() MethodThe reduce() method executes a reducer function on each element of the array, resulting in a single output value. It's ideal for summing up array values.Example: This example shows the use of reduce method to get the sum and average of an array. JavaScript const array = [10, 20, 30, 40, 50]; const sum = array.reduce((accumulator, currentValue) => accumulator + currentValue, 0); const average = sum / array.length; console.log("Sum:", sum); console.log("Average:", average); OutputSum: 150 Average: 30 Using forEach() MethodThe forEach() method executes a provided function once for each array element. You can use it to calculate the sum and then compute the average.Example: This example shows the use of forEach() method to get the sum and average of an array. JavaScript const array = [10, 20, 30, 40, 50]; let sum = 0; array.forEach((element) => { sum += element; // Accumulate sum }); const average = sum / array.length; console.log("Sum:", sum); console.log("Average:", average); OutputSum: 150 Average: 30 Using map() and reduce() MethodsThe map() method creates a new array populated with the results of calling a provided function on every element in the calling array. You can chain map() with reduce() to get the sum and compute the average.Example: This example shows the use of map() and reduce() methods to get the sum and average of an array. JavaScript const array = [10, 20, 30, 40, 50]; const sum = array.map((element) => element).reduce((acc, curr) => acc + curr, 0); const average = sum / array.length; console.log("Sum:", sum); console.log("Average:", average); OutputSum: 150 Average: 30 Comment More infoAdvertise with us Next Article How to Compute the Sum and Average of Elements in an Array in JavaScript? A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to compute the average of an array after mapping each element to a value in JavaScript ? Given an array, the task is to compute the average of an array after mapping each element to a value. Input : arr=[2, 3, 5, 6, 7] Output: 4.6 Explanation : (2+3+5+6+7)/5 = 4.6 Input : [5,7,2,7,8,3,9,3] Output: 5.5 Explanation : (5+7+2+7+8+3+9+3)/8 = 5.5 Here are some common approaches: Table of Cont 3 min read How to Get the Size of an Array in JavaScript To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that 2 min read Find the OR and AND of Array elements using JavaScript Given an array and the is task to find the OR and AND operations of the values of an Array using JavaScript. These are the following methods: Using simple for loopUsing reduce() MethodApproach 1: Using simple for loop It uses a simple method to access the array elements by an index number and use th 2 min read How to calculate the XOR of array elements using JavaScript ? In this article, we are given an array, and the task is to find the XOR of Array elements using JavaScript. There are two approaches to solve this problem which are discussed below: Method 1: It uses a simple method to access the array elements by an index number and use the loop to find the XOR of 2 min read How to Add the Numbers in a JavaScript Array? Adding the numbers in a JavaScript array is a common operation, particularly in tasks involving data aggregation or mathematical calculations. This process involves iterating through the array and summing its elements. JavaScript provides multiple methods to achieve this, ranging from traditional lo 2 min read How to Find the Sum of an Array of Numbers in JavaScript ? To find the sum of an array of numbers in JavaScript, you can use several different methods.1. Using Array reduce() MethodThe array reduce() method can be used to find the sum of array. It executes the provided code for all the elements and returns a single output.Syntaxarr.reduce( callback( acc, el 2 min read How to find an average color of an image using JavaScript ? The average color of an image can be found with JavaScript by drawing the image on a canvas element. The following steps have to be performed to get the average color of an image: 1. The image is first drawn on the canvas using the method context.drawImage() of the Canvas 2D API. This method takes i 3 min read How to Find the Length of an Array in JavaScript ? JavaScript provides us with several approaches to count the elements within an array. The length of an array lets the developer know whether an element is present in an array or not which helps to manipulate or iterate through each element of the array to perform some operation. Table of Content Usi 3 min read How to Change Values in an Array when Doing foreach Loop in JavaScript ? The forEach() method in JavaScript is a popular way to iterate through an array. Unlike other looping methods, forEach() passes a callback function for each element in the array. While forEach() itself does not directly change the array, you can modify elements within the array by using the callback 3 min read Sum and Product of Array elements using JavaScript Given an array and is the task to find the Sum and Product of the values of an Array using JavaScript. Below are the approaches to find the sum and product of array elements using JavaScript:Table of ContentUsing Iterative methodUsing reduce() methodUsing Iterative methodIt uses a simple method to a 3 min read Like