How to Add the Numbers in a JavaScript Array? Last Updated : 07 Jun, 2024 Summarize Comments Improve Suggest changes Share 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 Push an Array into Object in JavaScript? 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 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 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 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 How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati 4 min read Like