➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Array Addition in JavaScript | The array.reduce(callback, initialValue) method in JavaScript can retrieve the sum of an array’s elements. With the result of the most recent callback function call as the first parameter and the present array element as the second argument, the array.reduce() method calls the callback function once for each array component.
The array.reduce() method’s output will be the sum of the array’s items. Alternatively, you can use a “for” loop to determine the total array elements (see the example below). The reduce() method is used in this JavaScript Array Sum Example to calculate the sum of the array’s elements. More examples of summarizing a JavaScript array with descriptions are provided below. Also see:- JavaScript Array pop() Method
Example-1: Array Addition in JavaScript
let array = [1, 2, 3];
let result = array.reduce((a, b) => {
return a + b;
});
console.log("The result is:", result);
Output:-
The result is: 6
We can also use the for loop to add the array in JavaScript. This method involves adding each thing one at a time, iterating until the last item is added.
Example-2: Addition of Array Elements In JavaScript
function sumArray(array) {
let sum = 0; // the sum is initialed to 0
// JS arrays are zero-index based
// ourArray.length = 5, the initialization block is 0.
// the last item is index 4 which is < 5 (what we define in the condition block)
for (let i = 0; i < array.length; i += 1) {
// take every item in the array and add it to the sum variable
sum += array[i];
// initial: sum = 0
// iteration 1: 2 + 4 => sum = 6
// iteration 2: 6 + 6 => sum = 12
// iteration 3: 12 + 8 => sum = 20
// iteration 4: 20 + -2 => sum = 18
// iteration 5: 18 + -1 => sum = 17
// iteration 6: 17 + 0 => sum = 17
}
console.log("The sum is:", sum); // 17
return sum;
}
// call the function by passing array
sumArray([2, 4, 6, 8, -2, -1, 0]);
Output:-
The sum is: 17
We can also employ forEach loop to obtain the array addition in JavaScript. A built-in or array-like method is called forEach. As illustrated here, it is easy.
Example-3: Array Addition in JavaScript
function sumArray(array) {
let sum = 0;
// Go through each item in the array and execute
// this function which adds each item to sum
array.forEach((item) => {
sum += item;
});
console.log("The sum is:", sum);
return sum;
}
sumArray([2, 4, 6, 8, -2, -1, 0]);
Output:-
The sum is: 17
JavaScript has several loops, including the “for” loop and the reduce() method for adding an array of numbers. The repetitive nature of loops, which aim to take into account each element one by one, is advantageous when calculating the sum of an array. We have supplied the syntax and usage of the for-loop and reduce() methods to add all the numbers in an array in JavaScript.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!