Sum of array JavaScript | Add array elements example

Use the reduce() inbuild method to get the sum of an array in JavaScript. This method reduces the array to a single value.

Note:

  1. The method will not execute the function for array elements without values.
  2. This method does not change the original array.

Example of JavaScript sum array

Here is demonstrated HTML example code of JavaScript sum array. If the array is empty and no initialValue is provided, an error will be thrown.

Output:

Sum of array JavaScript

Do comment if you have any questions and doubts on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.