How to Add the Numbers in a JavaScript Array? Last Updated : 07 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Adding the numbers in a JavaScript array is a common operation, particularly in tasks involving data aggregation or mathematical calculations. This process involves iterating through the array and summing its elements. JavaScript provides multiple methods to achieve this, ranging from traditional loops to more modern and concise array methods. Table of Content Using a For LoopUsing a While LoopUsing Array.prototype.reduce()Using For-Of LoopUsing a For LoopThe traditional for loop is one of the most direct way to iterate over an array and sum its elements. You initialize a sum variable to zero, then iterate through each element of the array, adding each element to the sum. Example: To demonstrate the sum of all elements in an array using a simple loop. JavaScript let array = [1, 2, 3, 4, 5]; let sum = 0; for (let i = 0; i < array.length; i++) { sum += array[i]; } console.log(sum); Output15 Using a While LoopA while loop can also be used to sum the elements of an array. This method involves initializing a counter and a sum variable, then using the while loop to iterate through the array elements until the counter reaches the array's length. Example: To demonstrate the sum of array elements using a while loop. JavaScript let array = [1, 2, 3, 4, 5]; let sum = 0; let i = 0; while (i < array.length) { sum += array[i]; i++; } console.log(sum); Output15 Using Array.prototype.reduce() methodThe reduce() method provides a functional programming approach to summing array elements. This method takes a reducer function that accumulates the sum of the array elements and an initial value for the accumulator. Example: To illustrate the use of `reduce()` method to compute the sum of all elements in an array and then prints the result. JavaScript let array = [1, 2, 3, 4, 5]; let sum = array.reduce((accumulator, currentValue) => accumulator + currentValue, 0); console.log(sum); Output15 Using For-Of LoopFor-of loop is a modern and concise way to iterate over array elements. This loop directly iterates through the values of the array, making it easy to sum the elements. Example: To demonstrate the sum of all elements in an array using a for-of loop and prints the result. JavaScript let array = [1, 2, 3, 4, 5]; let sum = 0; for (let value of array) { sum += value; } console.log(sum); Output15 Comment More infoAdvertise with us Next Article How to Add the Numbers in a JavaScript Array? A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Web technologies Similar Reads How to Find the Sum of an Array of Numbers in JavaScript ? To find the sum of an array of numbers in JavaScript, you can use several different methods.1. Using Array reduce() MethodThe array reduce() method can be used to find the sum of array. It executes the provided code for all the elements and returns a single output.Syntaxarr.reduce( callback( acc, el 2 min read How numbers are stored in JavaScript ? In this article, we will try to understand how numbers are stored in JavaScript. Like any other programming language, all the data is stored inside the computer in the form of binary numbers 0 and 1. Since computers can only understand and process data in the form of 0's and 1's. In JavaScript, ther 6 min read How to Get the Size of an Array in JavaScript To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that 2 min read Print all Positive Numbers in JavaScript Array In JavaScript, arrays are the data structures used to store multiple elements. In some cases, we need to extract the elements that satisfy a specified condition. We can use a condition to extract all the positive numbers from an array with the below approaches. Example:Input: arr = [-1, -12, 35, -8, 3 min read How to Store an Object Inside an Array in JavaScript ? Storing an object inside an array in JavScript involves placing the object as an element within the array. The array becomes a collection of objects, allowing for convenient organization and manipulation of multiple data structures in a single container. Following are the approaches through which it 3 min read How to Add a Number to Every Item in an Array? Adding a number to every item in an array is a common operation in JavaScript, often required for tasks like incrementing values or performing mathematical operations on array elements. Below are the approaches to add a number to every item in an array:Table of ContentUsing Array.map() MethodUsing a 3 min read How to Find the Length of an Array in JavaScript ? JavaScript provides us with several approaches to count the elements within an array. The length of an array lets the developer know whether an element is present in an array or not which helps to manipulate or iterate through each element of the array to perform some operation. Table of Content Usi 3 min read How to Loop Through an Array in JavaScript? Here are the various ways to loop through an array in JavaScript1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other 4 min read How to Add Days to Date in JavaScript? Adding days to a date in JavaScript is a common task when working with dates and times. This involves manipulating the Date object to calculate a future or past date by adding a specific number of days. This is useful for tasks like scheduling events, and deadlines, or simply adjusting dates dynamic 2 min read How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one 2 min read Like