Sum of Array Elements

Program to calculate sum of all array elements

BeginnerTopic: Array Programs
Back

JavaScript Sum of Array Elements Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Using for loop
function sumArray(arr) {
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}

let numbers = [1, 2, 3, 4, 5];
console.log("Array:", numbers);
console.log("Sum:", sumArray(numbers));

// Method 2: Using reduce
function sumArrayReduce(arr) {
    return arr.reduce((acc, num) => acc + num, 0);
}

console.log("\nUsing reduce:", sumArrayReduce([10, 20, 30]));

// Method 3: Using forEach
function sumArrayForEach(arr) {
    let sum = 0;
    arr.forEach(num => sum += num);
    return sum;
}

console.log("\nUsing forEach:", sumArrayForEach([5, 10, 15]));

// Method 4: Using for...of loop
function sumArrayForOf(arr) {
    let sum = 0;
    for (let num of arr) {
        sum += num;
    }
    return sum;
}

console.log("\nUsing for...of:", sumArrayForOf([2, 4, 6, 8]));

// Method 5: One-liner
const sum = arr => arr.reduce((a, b) => a + b, 0);

console.log("\nOne-liner:", sum([1, 2, 3, 4, 5]));

// Method 6: Sum with condition (even numbers only)
function sumEvenNumbers(arr) {
    return arr.filter(num => num % 2 === 0)
              .reduce((acc, num) => acc + num, 0);
}

console.log("\nSum of even numbers:", sumEvenNumbers([1, 2, 3, 4, 5, 6]));
Output
Array: [ 1, 2, 3, 4, 5 ]
Sum: 15

Using reduce: 60

Using forEach: 30

Using for...of: 20

One-liner: 15

Sum of even numbers: 12

Understanding Sum of Array Elements

This program demonstrates different methods to sum array elements.

Method 1: For Loop

Traditional approach:

let sum = 0;
for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
}

Method 2: Reduce

Most common modern approach:

arr.reduce((acc, num) => acc + num, 0);

Initial Value:

0 is the starting accumulator value
Important for empty arrays

Method 3: ForEach

Functional iteration:

let sum = 0;
arr.forEach(num => sum += num);

Method 4: For...Of Loop

ES6 iteration:

for (let num of arr) {
    sum += num;
}

For...Of vs For Loop:

For...of: Cleaner, works with any iterable
For loop: More control, index access

Method 5: Arrow Function

One-liner:

const sum = arr => arr.reduce((a, b) => a + b, 0);

Method 6: Conditional Sum

Sum only even numbers:

arr.filter(num => num % 2 === 0)
   .reduce((acc, num) => acc + num, 0);

Chaining Methods:

filter(): Creates new array with matching elements
reduce(): Sums filtered array

Performance:

All methods: O(n) time complexity
Reduce: Slightly more efficient (single pass)

When to Use:

-

Reduce

: Most common, functional

-

For loop

: Learning, custom logic

-

For...of

: Clean iteration

-

One-liner

: Quick calculations

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Table of Contents