The Wayback Machine - https://web.archive.org/web/20190722135038/https://www.geeksforgeeks.org/javascript-array-reduce-method/amp/

JavaScript | Array reduce() Method

The array reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator.

Syntax:

array.reduce( function(total, currentValue, currentIndex, arr), initialValue )

Parameter: This method accepts two parameters as mentioned above and described below:

Example 1: This example use reduce() method to return the sum of all array elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Array reduce() Method
    </title>
</head>
  
<body style="text-align:center;">
      
    <h2>GeeksForGeeks</h2>
      
    <p>
        Click here to get the sum
        of array elements
    </p>
      
    <button onclick="myGeeks()">
        Click Here!
    </button>
      
    <br><br>
      
    Sum: <span id="GFG"></span>
      
    <!-- Script to use reduce method -->
    <script>
        var arr = [10, 20, 30, 40, 50, 60];
   
        function sumofArray(sum, num) {
            return sum + num;
        }
        function myGeeks(item) {
            document.getElementById("GFG").innerHTML
                    = arr.reduce(sumofArray);
        }
    </script>
</body>
  
</html>                    
chevron_right

Output:
Before click on the button:

After click on the button:

Example 2: This example use reduce() method to return the round sum of all array elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Array reduce() Method
    </title>
</head>
  
<body style="text-align:center;">
      
    <h2>GeeksForGeeks</h2>
      
    <p>
        Click here to get the sum
        of array elements
    </p>
      
    <button onclick="myGeeks()">
        Click Here!
    </button>
      
    <br><br>
      
    Sum: <span id="GFG"></span>
      
    <!-- Script to use reduce method -->
    <script>
        var arr = [1.5, 20.3, 11.1, 40.7];
   
        function sumofArray(sum, num) {
            return sum + Math.round(num);
        }
        function myGeeks(item) {
            document.getElementById("GFG").innerHTML
                    = arr.reduce(sumofArray, 0);
        }
    </script>
</body>
  
</html>                    
chevron_right

Output:
Before click on the button:

After click on the button:




Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :