Get Sum Of Array Of Numbers In JavaScript

To get sum of array of numbers in JavaScript you can use any of these methods –

  1. A basic for loop,
  2. The reduce() method,
  3. The eval() method.

Let’s look at each of them in detail.

Get Sum Of Array Of Numbers In JavaScript

get sum of array of numbers in javascript

1. A Basic for Loop

This is the most basic method you can use to sum up all the numbers of an array. You can do this by using a for loop and iterating through each element of the array, adding it to a total sum variable.

Example:

var arr = [-2,1,4];

var sum = 0;

for(var i=0;i<arr.length;i++){

  sum += arr[i];

}

console.log(sum); // Output: 3

In the above code, we have declared an array named arr. We have also initialized a sum variable to 0. Then we use a for loop to iterate through each element of the array and add it to the sum variable. Finally, we print the value of the sum variable on the console.

2. The reduce() Method

The reduce() method is a higher order function that takes an array and reduces it down to a single value. This method iterates through the array, performing a callback function on each element. The callback function takes two arguments – the previous value and the current value.

The return value of the callback function is used as the new previous value and this process continues until there is only one value remaining, which is then returned.

Example:

var arr = [-2,1,4];

var sum = arr.reduce(function(a,b){

  return a+b;

},0);

console.log(sum); // Output: 3

In the above code, we have declared an array named arr. We use the reduce() method on this array and pass in a callback function as an argument. The callback function takes two arguments – a and b. These arguments represent the previous value and the current value respectively.

The return value of the callback function is used as the new previous value. This process continues until there is only one value remaining, which is then returned.

The second argument to the reduce() method is an initial value that is used as the first previous value. In our case, we have set it to 0.

3. forEach Loop Method

This is yet another method to sum up all the numbers of an array. The forEach() method takes a callback function as an argument. This callback function is executed once for each element in the array.

We can use this callback function to add up all the elements of the array and assign it to a sum variable which can be printed later.

Example:

var arr = [-2,1,4];

var sum = 0;

arr.forEach(function(element){

  sum += element;

});

console.log(sum); // Output: 3

In the above code, we have declared an array named arr. We use the forEach() method on this array and pass in a callback function as an argument. The callback function takes an element as an argument.

This function is executed once for each element in the array. We use this callback function to add up all the elements of the array and assign it to a sum variable which can be printed later.

Conclusion

These are some of the methods you can use to get sum of array of numbers in JavaScript. You can choose any method depending on your requirements. I hope this article was helpful. Thank you for reading!

Leave a Reply