• 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 / Subtract All Numbers in an Array Using JavaScript

Subtract All Numbers in an Array Using JavaScript

September 16, 2022 Leave a Comment

To subtract all of the numbers of an array in JavaScript, we can use the JavaScript Array reduce() method. Here is the setup for how this can be done:

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function subtractArray(total, item) {
  return total - item;
}

console.log(numbers.reduce(subtractArray));

#Output
-43

We can also use a for loop to subtract all of the numbers of an array easily.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var total = numbers[0];
for( var i =1; i<numbers.length; i++ ){
  total -= numbers[i];
}

console.log(total);

#Output
-43

When working with collections of data in JavaScript, the ability to summarize the data easily is valuable.

One such case is if you want to subtract all numbers of an array.

To subtract all of the numbers of an array in JavaScript, the easiest way is with the reduce() method.

Below again we show you how to subtract all of the numbers of an array in JavaScript.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function subtractArray(total, item) {
  return total - item;
}

console.log(numbers.reduce(subtractArray));

#Output
-43

Using a For Loop to Subtract All Numbers in an Array in JavaScript

Another way we can subtract all of the numbers of an array is with a for loop.

To subtract all of the numbers of an array, initialize a variable to the first number of the array, and then subtract each element from that number while keeping the running total.

Below shows you how to subtract all of the numbers of an array in JavaScript with a for loop.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var total = numbers[0];
for( var i =1; i<numbers.length; i++ ){
  total -= numbers[i];
}

console.log(total);

#Output
-43

We can put our code in a function so that we can reuse it as often as we like.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

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

console.log(subtractArray(numbers));

#Output
-43

Hopefully this article has been useful for you to learn how to subtract all numbers in an array in JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Scroll to Bottom of Div
  • 2.  How in JavaScript to Get the Day of the Week
  • 3.  How to Check if a Number is Even or Odd in JavaScript
  • 4.  Using JavaScript to Convert a String to Uppercase
  • 5.  JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling
  • 6.  Creating a JavaScript Function to Multiply Two Numbers
  • 7.  Using JavaScript to Create an Empty Array
  • 8.  JavaScript atan2 – Find Arctangent of the Quotient of Two Numbers
  • 9.  Remove the Last Element From Array in JavaScript
  • 10.  JavaScript String endsWith Method

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