JavaScript Program to Find Average of Array
To find the average of an array in JavaScript, sum all elements using
reduce and then divide by the array's length like this: const average = arr.reduce((a, b) => a + b, 0) / arr.length;.Examples
Input[5]
Output5
Input[1, 2, 3, 4, 5]
Output3
Input[]
OutputNaN
How to Think About It
To find the average, first add all numbers in the array together, then divide that total by how many numbers there are. This gives the central value representing the array.
Algorithm
1
Get the input array of numbers.2
Add all the numbers together to get the sum.3
Count how many numbers are in the array.4
Divide the sum by the count to get the average.5
Return or print the average.Code
javascript
const arr = [1, 2, 3, 4, 5]; const sum = arr.reduce((a, b) => a + b, 0); const average = sum / arr.length; console.log(average);
Output
3
Dry Run
Let's trace the array [1, 2, 3, 4, 5] through the code
1
Start with array
arr = [1, 2, 3, 4, 5]
2
Calculate sum using reduce
sum = 1 + 2 + 3 + 4 + 5 = 15
3
Calculate average
average = sum / arr.length = 15 / 5 = 3
4
Print average
Output: 3
| Iteration | Accumulator (a) | Current Value (b) | Sum So Far |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
| 2 | 1 | 2 | 3 |
| 3 | 3 | 3 | 6 |
| 4 | 6 | 4 | 10 |
| 5 | 10 | 5 | 15 |
Why This Works
Step 1: Sum all elements
The reduce method adds each number in the array to an accumulator, starting from 0.
Step 2: Count elements
The length of the array tells us how many numbers there are to average.
Step 3: Divide sum by count
Dividing the total sum by the number of elements gives the average value.
Alternative Approaches
for loop sum
javascript
const arr = [1, 2, 3, 4, 5]; let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; } const average = sum / arr.length; console.log(average);
Uses a simple loop instead of reduce; easier to understand for beginners but more code.
for...of loop
javascript
const arr = [1, 2, 3, 4, 5]; let sum = 0; for (const num of arr) { sum += num; } const average = sum / arr.length; console.log(average);
Cleaner loop syntax; good readability and same performance.
using array length check
javascript
const arr = [1, 2, 3, 4, 5]; if (arr.length === 0) { console.log('Array is empty'); } else { const sum = arr.reduce((a, b) => a + b, 0); const average = sum / arr.length; console.log(average); }
Adds safety for empty arrays to avoid dividing by zero.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through the array once to sum all elements, so it takes time proportional to the number of elements, O(n).
Space Complexity
It uses a fixed amount of extra space for sum and average variables, so space complexity is O(1).
Which Approach is Fastest?
All approaches (reduce, for loop, for...of) have the same time and space complexity; choice depends on readability and style.
| Approach | Time | Space | Best For |
|---|---|---|---|
| reduce method | O(n) | O(1) | Concise code, functional style |
| for loop | O(n) | O(1) | Beginners learning loops |
| for...of loop | O(n) | O(1) | Readable and modern syntax |
Always check if the array is empty before dividing to avoid errors.
Beginners often forget to handle empty arrays, causing division by zero and NaN results.