
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the Product of Array Elements with Reduce in JavaScript
Problem
We are required to write a JavaScript function that takes in an array arr. Our function should find and return the product of all the elements of the array.
Example
Following is the code −
const arr = [3, 1, 4, 1, 2, -2, -1]; const produceElements = (arr = []) => { const res = arr.reduce((acc, val) => { acc = acc * val; return acc; }, 1); return res; }; console.log(produceElements(arr));
Output
Following is the console output −
48
Advertisements