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:Table of ContentUsing Iterative methodUsing reduce() methodUsing Iterative methodIt 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() methodThe 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(); OutputSum: 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(); OutputProduct: 39916800 Comment More infoAdvertise with us Next Article Sum and Product of Array elements using JavaScript S SHUBHAMSINGH10 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads Find the OR and AND of Array elements using JavaScript Given an array and the is task to find the OR and AND operations of the values of an Array using JavaScript. These are the following methods: Using simple for loopUsing reduce() MethodApproach 1: Using simple for loop It uses a simple method to access the array elements by an index number and use th 2 min read How to cartesian product of 2 arrays using JavaScript ? The task is to compute the cartesian product of two JavaScript arrays with the help of JavaScript. Here are a few techniques discussed. Approach 1: Create a new array.Traverse the first array by the outer loop and the second array by the inner loop.In the inner loop, Concatenate the first array elem 2 min read Product of an Array in JS or JavaScript We will be given an initial array and we have to multiply all the elements of the array with each other to find a final product.Examples: Input: arr = [1, 2, 3, 4, 5];Output: 120Explanation: 1 * 2 * 3 * 4 * 5 = 120Input: arr = [11, 12, 13, 14, 15]Output: 360360Explanation: 11 * 12 * 13 * 14 * 15 = 3 4 min read Sum of Squares of Even Numbers in an Array using JavaScript JavaScript program allows us to compute the sum of squares of even numbers within a given array. The task involves iterating through the array, identifying even numbers, squaring them, and summing up the squares. There are several approaches to find the sum of the square of even numbers in an array 2 min read Sum of an Array in JavaScript There are multiple ways to calculate the sum of elements in an array in JavaScript. Here are some of the most common methods:1. Using for loop (Simple for all Array)A basic and straightforward way to sum the elements of an array is by using a for loop. This method iterates through each element in th 4 min read How to calculate the XOR of array elements using JavaScript ? In this article, we are given an array, and the task is to find the XOR of Array elements using JavaScript. There are two approaches to solve this problem which are discussed below: Method 1: It uses a simple method to access the array elements by an index number and use the loop to find the XOR of 2 min read Javascript Program for Minimum product subset of an array Given an array a, we have to find the minimum product possible with the subset of elements present in the array. The minimum product can be a single element also.Examples: Input : a[] = { -1, -1, -2, 4, 3 }Output : -24Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24Input : a[] = 3 min read How to map, reduce and filter a Set element using JavaScript ? The map(), reduce() and filter() are array functions that transform the array according to the applied function and return the updated array. They are used to write simple, short and clean codes for modifying an array instead of using the loops. map() method: It applies a given function on all the e 2 min read Convert an Array into Array of Subarrays using JavaScript Given an array Num, our task is to convert it into an array of subarrays. Each subarray should contain a single element from the original array. we are going to discuss different approaches for solving this problem.Example:Input: Num=[1, 2, 3, 4] Output:[[1], [2], [3], [4]]Below are the approaches t 3 min read RMS Value Of Array in JavaScript The RMS (Root mean square) value of a distribution is the square root of the mean of the squares of the elements. The formula to find RMS value is given below: RMS = \sqrt{\frac{{a_{1}}^{2} + {a_{2}}^{2} + {a_{3}}^{2} + {a_{4}}^{2} + ... + {a_{N}}^{2}}{N}} To calculate the RMS value of an array, fir 3 min read Like