. Advertisement .
..3..
. Advertisement .
..4..
Do you want to know how to get the sum of an array of numbers in javascript? Don’t be alarmed! In this article, we’ll explain what’s causing it and offer some solutions to fix it. Let’s get this party started!
The way to get the sum of an array of numbers in Javascript?
Method 1: Utilize reduce() Method
reduce() iterates through the array in a loop and invokes the reducer function to save the results of the function’s array processing in an accumulator. When looping through an array, the results are aggregated and stored in an accumulator, a variable that is remembered during all iterations. Using this, we can iterate through the array, add the value of each element to the accumulator, and then calculate the array’s sum.
This is the syntax of reduce() method:
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
function(): essential. a process that will be executed for every element in the array.
Total: Mandatory. The function’s initialValue, or the last value it returned.
currentValue: Required. The current element’s value.
currentIndex: Not required. The current element’s index.
Arr: Elective. The array that contains the current element.
As shown in the example below, the reduce() method applies the supplied reducer function to every element of the array, producing a single output value:
const arr = [1, 2, 3, 4];
const reducer = (accumulator, curr) => accumulator + curr;
console.log(arr.reduce(reducer));
Method 2: Utilize The for loop
It can be used to add every integer in an array and then save the result in a variable.
for (expression 1; expression 2; expression 3)
Before the code block is executed, Expression 1 is run (one).
The prerequisite for running the code block is specified by expression 2.
After executing the code block, Expression 3 is run (each time).
Take a look at the example below:
let numbers = [10, 20, 30, 40, 50]
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i]
}
console.log(sum)
When using the for loop to visit each element and add it to the array’s total, we initialize the sum variable with a value of 0 to store the outcome.
The output of this command will be:
> 150
Method 3: Utilize for…of
for (variable of iterable)
Variable: The variable receives the value of the following property for each iteration.
Iterable: an object with iterable attributes.
We can use it to obtain an array’s sum because it allows us to iterate across an array. Then copy and modify as we use the same style of functions above.
function sumArray(array) {
let sum = 0;
/*loop over array and add each item to sum
*/
for (const item of array) {
sum += item;
}
// return the result
console.log(sum);
return sum;
}
sumArray([1, 4, 0, 9, -3]); //logs 11
Conclusion
We hope you have figured out how to get the sum of an array of numbers in javascript. We expect the article also explains how to do it. If you have any further questions about this question, please leave a comment below, and we will respond as soon as possible! We appreciate the time you took to read.
Read more
→ How To Get A Random Element From An Array Using JavaScript
Leave a comment