Register to get access to free programming courses with interactive exercises

Aggregation JS: Arrays

One reason loops are used with arrays is aggregation. Aggregation is any calculation that is, as a rule, based on the entire data set, such as finding the maximum, average, sum, etc. of all the data. The aggregation process does not require knowledge of new syntax, but it affects the problem-solving algorithm. Therefore, it makes sense to consider it separately. Let's start by looking for the maximum.

const calculateMax = (coll) => {
  // If the collection is empty, it cannot have a max value
  // In such situations, it usually returns null
  // This is a classic example of the guard expression idiom
  if (coll.length === 0) {
    return null;

  // The element comparison should start with a first element
  let max = coll[0]; // Take the first element as the maximum
  // Start the traversal with the second element
  for (let i = 1; i < coll.length; i += 1) {
    const currentElement = coll[i];
    // If the current element is larger than the maximum one,
    // it becomes the maximum
    if (currentElement > max) {
      max = currentElement;

  // Don't forget to return the maximum number
  return max;

console.log(calculateMax([])); // => null
console.log(calculateMax([3, 2, -10, 38, 0])); // => 38

Why is this an example of aggregation? Here we see a calculation that involves comparing all the elements to find the one that will be the result of this operation.

Note that the initial value of max is the first element, not, say, the number 0. After all, it may turn out that all numbers in the array are less than 0, and then we will get the wrong answer.

Now consider searching for the sum:

const calculateSum = (coll) => {
  // Initial value of the sum
  let sum = 0;
  for (let i = 0; i < coll.length; i += 1) {
    // Add all elements one by one
    sum += coll[i];

  return sum;

// The sum of elements always returns a number
// If the array is empty, the sum of its elements is 0
console.log(calculateSum([])); // => 0

console.log(calculateSum([3, 2, -10, 38, 0])); // => 33
// Calculation process
let sum = 0;
sum = sum + 3; // 3
sum = sum + 2; // 5
sum = sum + -10; // -5
sum = sum + 38; // 33
sum = sum + 0; // 33

The algorithm for finding the sum is much simpler, but has a couple of important nuances.

What is the sum of the elements of the empty array? As far as mathematics is concerned, such a sum equals 0. Seems like common sense. If we have no apples, then we have 0 apples (the number of apples is zero). Functions in programming work according to this logic.

The second point has to do with the initial element of the sum. The variable sum has an initial value of 0. Why set a value at all? Any repetitive operation starts with a value. You can't just declare a variable and start working with it inside a loop. This will lead to an incorrect result:

  // the initial value is not set
  // js automatically makes it equal to undefined
  let sum;

  // the first iteration of the loop
  sum = sum + 2; // ?

The result of this call will be NaN, i.e., a not-a-number, inside sum. It comes from trying to add 2 and undefined. So a value is needed after all. Why is 0 selected in the code above? It is very straightforward to check that all other options will lead to the wrong result. If the initial value is 1, then the result is 1 higher than it should be.

In mathematics, there is a concept of a neutral or identity element of an operation (each operation has its own element). This concept has a very simple meaning. An operation with this element does not change the value on which the operation is performed. In addition, any number plus zero gives the number itself. The same applies for subtraction. Even concatenation has a neutral element, which is an empty string: '' + 'one' will be 'one'.

Aggregation does not always mean that a collection of elements is reduced to a simple value. The result of aggregation can be any complex structure, such as an array. Such examples are often found in real life. The simplest example is a list of unique words in a text.

Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
hours of theory

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions

Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.