Product of an Array in JS or JavaScript
Last Updated :
10 Nov, 2024
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: 120
Explanation: 1 * 2 * 3 * 4 * 5 = 120
Input: arr = [11, 12, 13, 14, 15]
Output: 360360
Explanation: 11 * 12 * 13 * 14 * 15 = 360360
For Loop
In this method, we will use a simple for loop in JavaScript to iterate through the elements of the array and multiply them with each other to get the multiply result at the end.
JavaScript
const arr1 = [8, 9, 3, 7, 5, 13];
const arr2 = [12, 9, 5, 18, 23];
const iterativeMultiply = (arr) => {
let result = 1;
for (let i = 0; i < arr.length; i++) {
result *= arr[i];
}
return result;
};
console.log(iterativeMultiply(arr1))
Using the Math Object:
JavaScript's Math object provides several methods for mathematical operations. We can use the Math object to multiply all the elements of an array using the Math.pow() method, which calculates the power of a number.
JavaScript
function multiplyArray(arr) {
return arr.reduce((acc, curr) => acc * curr, 1);
}
const array = [1, 2, 3, 4, 5];
console.log(multiplyArray(array));
Using the in-built methods:
There are some in-built array methods available in JavaScript which we can use to iterate through the array elements and make changes in their values by passing a callback function inside them. We will use the map() and the reduce() in-built methods in this approach to multiply the elements of an array.
JavaScript
let arr1 = [8, 9, 3, 7, 5, 13];
let arr2 = [12, 9, 5, 18, 23];
let mapRes = 1;
arr1.map((currItem) => {
mapRes *= currItem;
});
const reduceRes = arr2.reduce((res, currItem) => res *= currItem);
console.log(mapRes);
console.log(reduceRes);
Using forEach method
In this approach we use forEach method. With forEach we look at each number in the array, one after the other. for each number we multiply it with a running total.
JavaScript
function multiplyArray(arr) {
let result = 1;
arr.forEach(num => {
result *= num;
});
return result;
}
let array = [1, 2, 3, 4, 5];
console.log(multiplyArray(array));
Using every() Method:
In this approach, we will use the every() method to iterate through the array. This method is typically used for testing conditions, but we can repurpose it for side-effect operations by keeping a running total and multiplying each element of the array to the running total.
JavaScript
const arr1 = [8, 9, 3, 7, 5, 13];
const arr2 = [12, 9, 5, 18, 23];
const everyMultiply = (arr) => {
let result = 1;
arr.every(num => {
result *= num;
return true; // Continue to the next element
});
return result;
};
console.log(everyMultiply(arr1)); // 98280
console.log(everyMultiply(arr2)); // 223560
Using the reduce Method with Initial Value
The reduce method in JavaScript can efficiently multiply all elements of an array by accumulating the product of elements. By passing an initial value to reduce, you ensure that the accumulation starts correctly. This method is concise and leverages JavaScript's built-in functionalities for array manipulation.
Example Code:
JavaScript
function multiplyArrayElements(arr) {
return arr.reduce((accumulator, currentValue) => accumulator * currentValue, 1);
}
console.log(multiplyArrayElements([1, 2, 3, 4, 5]));
console.log(multiplyArrayElements([11, 12, 13, 14, 15]));
Recursive Method - Not Recommend
In this approach, we will recursively visit each element of the array and multiply it with the product of all the previous elements to get a new product value till the current element.
JavaScript
const arr1 = [8, 9, 3, 7, 5, 13];
const arr2 = [12, 9, 5, 18, 23];
const recursiveMultiply = (arr, ind) => {
if (ind === arr.length - 1) {
return arr[ind];
} else {
return arr[ind] * recursiveMultiply(arr, ind + 1);
}
};
console.log(recursiveMultiply(arr1, 0));
console.log(recursiveMultiply(arr2, 0));
Similar Reads
Set to Array in JS or JavaScript This article will show you how to convert a Set to an Array in JavaScript. A set can be converted to an array in JavaScript in the following ways:1. Using Spread OperatorThe JavaScript Spread Operator can be used to destructure the elements of the array and then assign them to a new variable in the
2 min read
How to Loop Through an Array in JavaScript? Here are the various ways to loop through an array in JavaScript1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other
4 min read
Sum and Product of Array elements using JavaScript 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 a
3 min read
Types of Arrays in JavaScript A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi
3 min read
JavaScript- Convert an Object to JS Array Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje
3 min read
JavaScript- Add an Object to JS Array In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively. These are the following ways to add an object to a JS array: Using JavaScript Array
4 min read