javascript

# Get the sum of array values using Javascript - Solved

To get the sum of array values which are integer type we can use .reduce() method of Javascript array to get this with one line of code.

``````const myarr = [10, 23, 32, 65, 7, 24];
const sum_of_array = myarr.reduce((a, b) => a + b);
console.log(sum_of_array);``````

Output

``161``

Try it yourself

We are getting the sum of "myarr" variable in the code snippet which contains 6 integer values. We are using .reduce() array method and adding its parameters.

## Get the sum of array values using reduce() function

The reduce() function is a powerful tool for manipulating arrays in JavaScript. It can be used to quickly and easily calculate the sum of all the values in an array.
This article will explain how to use the reduce() function to compute the sum of an array of values. It will also provide some examples to illustrate how reduce() function works.

Syntax

``array.reduce((a, b) => a + b)``

Example

``````const numbers = [1,2,3,4,5];

const sum = numbers.reduce((total, num) => total + num);

console.log(sum); //Output: 15``````

This code uses the reduce() method to sum up all of the numbers in the array 'numbers'. The first argument of reduce() is a callback function that takes two parameters: the first parameter, 'total', is the total sum of all of the numbers in the array, and the second parameter, 'num', is the current number in the array that is being added to the total.
The initial value of the total is 0. After the loop executes, the total sum of all of the numbers in the array is stored in the variable 'sum', and is logged to the console with console.log().