JavaScript Average of N Numbers Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Average of N Numbers?

The average (arithmetic mean) of N numbers is their sum divided by the count.

Remember
average = (a₁ + a₂ + … + aₙ) / N

Example: 1, 2, 3, 4, 5
  sum = 15, N = 5
  average = 15 / 5 = 3
ValuesSumNAverage
1, 2, 3, 4, 51553
10, 15, 20, 25, 30100520
2, 4623
1, 2321.5

How to Find the Average of N Numbers in JavaScript

Sum every value once, then divide by the count — guard empty input first.

  1. Reject N <= 0 or an empty array (no division by zero).
  2. Iterate the N values (loop or reduce).
  3. Accumulate a running sum.
  4. Return sum / N.
MethodIdeaTimeBest for
Running sum + NLoop N times with a fixed listO(N)Interviews, demos
Array helperSum values.length itemsO(N)Reusable default
Array.reduceSum in one expressionO(N)Concise scripts

Pseudocode

Pseudocode
function Average(values):
    n = length of values
    if n == 0:
        return error   // or a documented sentinel

    sum = 0
    for each value in values:
        sum = sum + value

    return sum / n

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Formulaaverage = sum / N
GuardNever divide when N <= 0
JS numbersDivision keeps decimals (no int truncation)
Not medianMedian needs sorting; average does not
1..5sum 15 → average 3

Live Preview

Enter comma-separated numbers to see the sum, count, and average.

Comma-separated numbers. Tap a chip or type your own list.

Result
values = [1, 2, 3, 4, 5]
sum = 15, N = 5
average = 3

JavaScript Programs

Start with a running-sum helper and fixed N. Then average any array by length, and finish with a concise reduce mean. Each example has a Try it Yourself editor.

Example 1 — Running Sum with Fixed N

Validate N, loop N times over a values list, keep a running total, then divide.

JavaScript
function averageOfNNumbers(n, values) {
  if (n <= 0) {
    return 0;
  }

  let total = 0;
  for (let i = 0; i < n; i++) {
    total += values[i];
  }
  return total / n;
}

const n = 5;
const values = [1, 2, 3, 4, 5];
const result = averageOfNNumbers(n, values);
console.log("The average of the entered numbers is: " + result);
Try It Yourself

How It Works

  1. Return 0 if n <= 0 (documented sentinel).
  2. Add values[0] through values[n - 1] into total.
  3. Divide once after the loop: total / n.
  4. JS numbers keep decimals, so 1 / 2 is 0.5, not truncated.
  5. Sample 1…5 prints 3.

Example 2 — Average from an Array

Reusable helper: sum every element, then divide by length.

JavaScript
function averageFromArray(values) {
  if (!values.length) {
    return 0;
  }

  let total = 0;
  for (let i = 0; i < values.length; i++) {
    total += values[i];
  }
  return total / values.length;
}

const data = [10, 15, 20, 25, 30];
const avg = averageFromArray(data);
console.log("Average = " + avg);
Try It Yourself

How It Works

  1. Reject an empty array before dividing.
  2. Sum every element in one forward pass.
  3. Divide by values.length for the mean.
  4. 10+15+20+25+30 = 100 → average 20.
  5. Prefer this shape in interviews: one clear helper.

Example 3 — Mean with Array.reduce

Same formula in one expression — still guard empty arrays.

JavaScript
function mean(values) {
  if (!values.length) {
    return 0;
  }
  return values.reduce(function (sum, x) {
    return sum + x;
  }, 0) / values.length;
}

const data = [1, 2, 3, 4, 5];
console.log(mean(data));
Try It Yourself

How It Works

  1. Return early when values.length is 0.
  2. reduce folds the list into a single sum starting at 0.
  3. Divide the sum by the length once.
  4. Same math as the for loop — just more compact.
  5. Sample 1…5 prints 3.

Edge Cases & Pitfalls

Check these before calling the solution done.

Empty

Never divide by N = 0

Empty lists are undefined. Return an error or a documented sentinel.

N = 1

Average = itself

A single value’s average is that value.

Negatives

Allowed

The formula works with negative numbers too.

Median

Different from median

Average is sum/count. Median needs a sorted middle value.

Time and Space Complexity

ProgramTimeExtra space
Running sum / array / reduceO(N)O(1) beyond the input
Storing all inputs firstO(N)O(N)

One pass is enough. You do not need to sort for an arithmetic mean.

Key Takeaways

  • Formula: average = sum / N.
  • Guard: never divide when N is zero.
  • JS: division keeps decimals; no cast needed for means.
  • Test: 1…5 → 3; 10…30 step 5 → 20; 1,2 → 1.5.

One line: add every value, divide by the count — that is the average.

Frequently Asked Questions

Add all N numbers, then divide by N. Formula: average = sum / N. Numbers in JS are floating-point by default, so decimals are kept.
Division by zero is undefined. If the count is 0 (empty list), do not compute an average.
No. Average is sum divided by count. Median is the middle value after sorting.
Yes. Sum with reduce, then divide by values.length. Still guard the empty-array case first.
O(N) — each value is visited once. A running sum uses O(1) extra space.
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.

Next: Biggest of Three Numbers

Find the largest among three values with simple comparisons.

Biggest of three tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful