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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  How to Use JavaScript to Change Text in Span
  • 2.  Get the Sum of Array in JavaScript
  • 3.  JavaScript value – Get the Value from an Input Field
  • 4.  Check if a String Contains Uppercase Letters in JavaScript
  • 5.  Math floor JavaScript – Using Math.floor() to Round Down to Floor
  • 6.  Remove Commas From Array in JavaScript
  • 7.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 8.  Swapping Images in JavaScript
  • 9.  JavaScript onfocus – Changing Form Styles with the onfocus Event
  • 10.  Using the appendChild Method with JavaScript to Add Items to a List

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