➤ How to Code a Game

➤ Array Programs in Java

➤ Java Inline Thread Creation

➤ Java Custom Exception

➤ Hibernate vs JDBC

➤ Object Relational Mapping

➤ Check Oracle DB Size

➤ Check Oracle DB Version

➤ Generation of Computers

➤ XML Pros & Cons

➤ Git Analytics & Its Uses

➤ Top Skills for Cloud Professional

➤ How to Hire Best Candidates

➤ Scrum Master Roles & Work

➤ CyberSecurity in Python

➤ Protect from Cyber-Attack

➤ Solve App Development Challenges

➤ Top Chrome Extensions for Twitch Users

➤ Mistakes That Can Ruin Your Test Metric Program

**JavaScript Average of Array** | The relevance of data is described by the average value. Or the average is the middle value of a bunch of numbers in mathematics. The mean, or mathematical mean, is another name for the average. We divide the total of all the entries by the number of values to get the average. In JavaScript, the average of an array is calculated as follows:

1) Add up all of the array’s values.

2) Divide the total by the array’s length.

For instance:-

array = 1, 2, 3, 4

avg= 2.5

We will learn how to find the average of an array in JavaScript in three distinct ways in this guide:

1) Using for loop

2) Using forEach() function

3) Using reduce() function

## JavaScript Average of Array Using for loop

Here’s how to find the average of an array of values using a for loop:-

1. Make a numerical array.

2. To add up the numbers, set up the sum variable.

3. Add each number to the sum as you loop over the numbers.

4. Divide the total number of numbers in the array by the total number of numbers in the array.

5. Display the outcome.

**Program for JavaScript Average of Array using for loop**

```
const arr = [1, 2, 3, 4, 5, 6, 7];
var sum = 0;
for (var number of arr) {
sum += number;
}
average = sum / arr.length;
console.log(average);
```

Output:-

4

## JavaScript Calculate Average Of Array Using forEach() Function

There is a built-in function called forEach() in JavaScript. For each entry in the array, this function is used to run a provided function.

To put it another way, the forEach() function is similar to a for loop. It traverses the array, calling the specified function for each element.

Using the forEach() function, determine the average:-

1. Make a numerical array.

2. Set the total to zero.

3. To loop through the integers and add each one to the sum, use the forEach() function.

4. Divide the total number of numbers in the array by the total number of numbers in the array.

ForEach() must take a function that takes a number input and adds it to the sum in order to add each number to the total.

**Program for JavaScript Average of Array using forEach()**

```
const arr = [1, 2, 3];
var sum = 0;
arr.forEach(function (num) { sum += num });
average = sum / arr.length;
console.log(average);
```

Output:-

2

## Find Average of Array Javascript Using reduce()

There is a built-in function named reduce() in JavaScript. In mathematics, this function is similar to folding. Folding a set of numbers into a single value is known as reducing.

To get the sum of an array of numbers, for example, you can reduce or fold it. A function is passed as an argument to the reduce() method. This is referred to as a reducer. It also accepts as its second parameter an initial value for the cumulative result.

The reduce() and reducer functions work on an array in the following way:

1. To get a partial result, use a reducer on the first two components of the array.

2. To make a new array, call the same function on the incomplete result and the third element of the array.

3. Continue until no more values are available.

4. Return the result.

**Program for JavaScript Average of Array using reduce()**

```
const arr = [2, 3, 1, 6, 8];
const average = arr.reduce((a, b) => a + b, 0) / arr.length;
console.log(average);
```

Output:-

4

You learned how to use JavaScript to calculate the average of an array. To summarise, in statistics, the average is used to describe the centrality of a dataset.

To calculate the average (mean), add the values together and divide the total by the number of values. There are several methods for calculating the average in JavaScript. Three frequent instances were shown in this guide:

- For loop
- The forEach() function
- The reduce() method

In the beginning, the latter method is a little more difficult to grasp. However, it is a really quick and easy approach to complete the task.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!