• 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
  • Ruby
  • 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.  Convert an Array to Set in JavaScript
  • 2.  How to Create a New h1 Element With JavaScript
  • 3.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss
  • 4.  Using JavaScript to Get Today’s Date
  • 5.  How to Find the Longest String in an Array in JavaScript
  • 6.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 7.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 8.  JavaScript sin – Find Sine of Number in Radians Using Math.sin()
  • 9.  Find Common Elements in Two Arrays Using JavaScript
  • 10.  Set Checkbox as Checked in JavaScript

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy