Open In App

Sum and Product of Array elements using JavaScript

Last Updated : 09 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array and is the task to find the Sum and Product of the values of an Array using JavaScript.

Below are the approaches to find the sum and product of array elements using JavaScript:

Using Iterative method

It uses a simple method to access the array elements by an index number and use the loop to find the sum and product of values of an Array using JavaScript.

Example 1: This example uses a simple method to find the sum of Array elements using JavaScript.

JavaScript
// Function to calculate the sum of an array
function sum(input) {
    if (toString.call(input) !== "[object Array]") return false;

    let total = 0;
    for (let i = 0; i < input.length; i++) {
        if (isNaN(input[i])) {
            continue;
        }
        total += Number(input[i]);
    }
    return total;
}

// Given array
let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

// Log the sum to the console
console.log("----Sum of Array----");
console.log(sum(array));

Output
----Sum of Array----
66

Example 2: This example uses a simple method to find the product of Array elements using JavaScript.

JavaScript
// Function to calculate the product of an array
function product(input) {
    if (toString.call(input) !== "[object Array]") return false;

    let total = 1;
    for (let i = 0; i < input.length; i++) {
        if (isNaN(input[i])) {
            continue;
        }
        total *= Number(input[i]);
    }
    return total;
}

// Given array
let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

// Log the product to the console
console.log("----Product of Array----");
console.log(product(array));

Output
----Product of Array----
39916800

Using reduce() method

The array reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator.

Syntax:

array.reduce( function( total, currentValue, currentIndex, arr ), initialValue )

Example 1: This example uses array reduce() method to find the sum of values of an Array using JavaScript.

JavaScript
// Given array
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

// Function to calculate the sum of two numbers
function sumofArray(sum, num) { 
    return sum + num; 
} 

// Function to log the sum of the array to the console
function myGeeks() { 
    const totalSum = arr.reduce(sumofArray, 0); // Initialize the sum with 0
    console.log("Sum:", totalSum); 
}

// Call the function to log the sum
myGeeks();

Output
Sum: 66

Example 2: This example uses array reduce() method to find the product of values of an Array using JavaScript.

JavaScript
// Given array
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

// Function to calculate the product of two numbers
function productofArray(product, num) { 
    return product * num; 
} 

// Function to log the product of the array to the console
function myGeeks() { 
    const totalProduct = arr.reduce(productofArray, 1); // Initialize the product with 1
    console.log("Product:", totalProduct); 
}

// Call the function to log the product
myGeeks();

Output
Product: 39916800



Next Article
Article Tags :

Similar Reads