How to Find the Sum of an Array of Numbers in JavaScript ? Last Updated : 11 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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, element, index, array ) , acc ) JavaScript // Given array let a = [ 10, 20, 30, 40, 50 ]; // Calculate the sum using array.reduce() let sum = a.reduce( (acc,e ) => acc + e , 0) // Display the final Output console.log("Sum of Array Elements: ", sum); OutputSum of Array Elements: 150 2. Using for LoopWe can also use for loop to find the sum of array elements. The for loop repeatedly executes a block of code as long as a specified condition is true.Syntaxfor ( Initialization ; condition ; increment){ // Code to be executed...} JavaScript // Given array let a = [10, 20, 30, 40, 50]; // Initialize sum variable let sum = 0; // Iterate using for loop for (let i = 0; i < a.length; i++) { // Add to the number in each iteration sum += a[i]; } // Display the final output console.log("Sum of Array Elements: ", sum); OutputSum of Array Elements: 150 Time complexity: O(N)Space complexity: O(1)3. Using eval() and join() MethodsThe eval() method is used to evaluate the expression string and array.join() method is used to create string from the array elements.Syntaxeval(string); JavaScript // Given an Array let a = [10, 20, 30, 40, 50]; // Use eval to calculate the sum let sum = eval(a.join('+')); // Display the final output console.log("Sum of Array Elements: ", sum); OutputSum of Array Elements: 150 Using forEach() MethodThe forEach() loop is used to iterate over the array and find the sum of array elements.Syntaxarray.forEach( callback(element, index, arr ), thisValue ); JavaScript // Given array let a = [10, 20, 30, 40, 50]; // Initialize sum variable let sum = 0; // Calculate the sum using forEach loop a.forEach( e => sum += e ); // Display the final output console.log("Sum of Array Elements: ", sum); OutputSum of Array Elements: 150 Comment More infoAdvertise with us Next Article How to Find the Sum of an Array of Numbers in JavaScript ? B bhushanc2003 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads 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 Add the Numbers in a JavaScript Array? 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 lo 2 min read Sum of Squares of Odd Numbers in an Array in JavaScript The task involves iterating through the array, identifying odd numbers, squaring them, and summing up the squares. The different approaches to accomplish this task are listed below.Table of ContentIterative ApproachUsing Array MethodsUsing map and reduce MethodsIterative ApproachIn this approach, we 3 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 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 get the standard deviation of an array of numbers using JavaScript ? Given an array and the task is to calculate the standard deviation using JavaScript. Example: Input: [1, 2, 3, 4, 5]Output: 1.4142135623730951Input: [23, 4, 6, 457, 65, 7, 45, 8]Output: 145.13565852332775Please refer to Mean, Variance, and Standard Deviation for details. Mean is average of element. 3 min read Sum of Squares of Even Numbers in an Array using JavaScript JavaScript program allows us to compute the sum of squares of even numbers within a given array. The task involves iterating through the array, identifying even numbers, squaring them, and summing up the squares. There are several approaches to find the sum of the square of even numbers in an array 2 min read Sum of an Array in JavaScript There are multiple ways to calculate the sum of elements in an array in JavaScript. Here are some of the most common methods:1. Using for loop (Simple for all Array)A basic and straightforward way to sum the elements of an array is by using a for loop. This method iterates through each element in th 4 min read How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read How to Compute the Sum and Average of Elements in an Array in JavaScript? Computing the sum and average of elements in an array involves iterating through the array, accumulating the sum of its elements, and then dividing the sum by the total number of elements to get the average. This can be achieved using various techniques, from basic loops to more advanced functional 3 min read Like