Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

JavaScript Array reduce() Method

Posted on December 31, 2024December 31, 2024 By Admin No Comments on JavaScript Array reduce() Method

The Array.reduce() method is a powerful JavaScript function that processes each element of an array to reduce it to a single output value. It iterates through the array while accumulating a result based on the logic provided in a callback function.

Syntax

arr.reduce(callback(accumulator, currentValue, index, array), initialValue);

Parameters

ParameterDescription
callback(Required) A function that executes on each element of the array.
accumulator(Required in callback) The accumulated result from previous callback executions.
currentValue(Required in callback) The current element being processed.
index(Optional in callback) The index of the current element.
array(Optional in callback) The array reduce() is being applied on.
initialValue(Optional) A value to use as the initial accumulator. If not provided, the first element of the array is used.

Return Value

The reduce() method returns the final accumulated value after iterating through all elements of the array.

Example 1: Summing Array Elements

let arr = [10, 20, 30, 40, 50];
let sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 150

Example 2: Multiplying Array Elements

let arr = [1, 2, 3, 4];
let product = arr.reduce((accumulator, currentValue) => accumulator * currentValue, 1);
console.log(product); // Output: 24

Example 3: Flattening an Array

let arr = [[1, 2], [3, 4], [5, 6]];
let flatArray = arr.reduce((accumulator, currentValue) => accumulator.concat(currentValue), []);
console.log(flatArray); 
// Output: [1, 2, 3, 4, 5, 6]

Example 4: Counting Occurrences

let arr = ['apple', 'banana', 'apple', 'cherry', 'banana', 'banana'];

let count = arr.reduce((accumulator, fruit) => {
    accumulator[fruit] = (accumulator[fruit] || 0) + 1;
    return accumulator;
}, {});

console.log(count); 
// Output: { apple: 2, banana: 3, cherry: 1 }

Example 5: Finding the Maximum Value

let arr = [10, 25, 5, 30, 15];

let max = arr.reduce((accumulator, currentValue) => 
    currentValue > accumulator ? currentValue : accumulator
);
console.log(max); // Output: 30

Supported Browsers

BrowserSupport
Chrome3+
Firefox3+
Safari4+
Edge12+
Opera10.5+
Internet Explorer9+
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array reverse() Method
Next Post: JavaScript Array reduceRight() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme