Sum of Array Elements
Program to calculate sum of all array elements
JavaScript Sum of Array Elements Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// 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]));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 valueMethod 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:
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 elementsreduce(): Sums filtered arrayPerformance:
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.
Practical Learning Notes for Sum of Array Elements
This JavaScript program is part of the "Array Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.