• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / Using JavaScript to Multiply All Elements in an Array

Using JavaScript to Multiply All Elements in an Array

June 7, 2022 Leave a Comment

In JavaScript, we can multiply all elements in an array easily by making use of a for loop. We simply loop through all elements in the array and multiply them together.

Here is some simple code to do this:

var array_of_numbers = [9,3,2,4];
var total = array_of_numbers[0];
for( var i=1; i<array_of_numbers.length; i++ ){
  total = total * array_of_numbers[i];
};

Let’s put our code above in a function so we can make it really easy to multiply all elements of an array.

function multiplyArray(arr){
  var total = arr[0];
  for( var i=1; i<arr.length; i++ ){
    total = total * arr[i];
  }
  return total;
};

In the above function multiplyArray, as you can see it takes only one parameter, arr, which is the array you want to multiply all elements of.

Finally, let’s see our function in action with an example:

function multiplyArray(arr){
  var total = arr[0];
  for( var i=1; i<arr.length; i++ ){
    total = total * arr[i];
  }
  return total;
};

var array_of_numbers = [9,3,2,4];

console.log(multiplyArray(array_of_numbers));

#Output:
216

When working with arrays of numbers, the ability to summarize the array and get certain statistics easily is valuable.

One such statistic is the product of all numbers in an array.

We can get the product of all numbers in an array easily in JavaScript. To get the product of numbers in an array, we can use a for loop and multiply each number by the cumulative product up to that point.

Here again is our function to multiply all elements of an array:

function multiplyArray(arr){
  var total = arr[0];
  for( var i=1; i<arr.length; i++ ){
    total = total * arr[i];
  }
  return total;
};

Hopefully this article has been useful for you to learn how to use JavaScript to multiply all elements in an array.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Round to 1 Decimal
  • 2.  Using JavaScript to Get the Last Day of the Month
  • 3.  JavaScript toLocaleString – Display the Date and Time Using JavaScript
  • 4.  Remove Undefined Values From an Array in JavaScript
  • 5.  JavaScript onkeydown – How to Use onkeydown Event with JavaScript
  • 6.  How to Create a JavaScript Countdown Timer
  • 7.  JavaScript onkeyup – How to Use onkeyup Event with JavaScript
  • 8.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 9.  How to Repeat Character N Times in JavaScript
  • 10.  Using JavaScript to Subtract Days From a Date

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy