Chrome Extension

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);
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

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

Below is a working demo of getting sum of array values.

Example Demo:

Was this helpful?
0 Comments