Skip to content

WebDevHubs

  • Home
  • JavaScript
  • 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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 150
let arr = [10, 20, 30, 40, 50]; let sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0); console.log(sum); // Output: 150
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [1, 2, 3, 4];
let product = arr.reduce((accumulator, currentValue) => accumulator * currentValue, 1);
console.log(product); // Output: 24
let arr = [1, 2, 3, 4]; let product = arr.reduce((accumulator, currentValue) => accumulator * currentValue, 1); console.log(product); // Output: 24
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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]
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]
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 }
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 }
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 25, 5, 30, 15];
let max = arr.reduce((accumulator, currentValue) =>
currentValue > accumulator ? currentValue : accumulator
);
console.log(max); // Output: 30
let arr = [10, 25, 5, 30, 15]; let max = arr.reduce((accumulator, currentValue) => currentValue > accumulator ? currentValue : accumulator ); console.log(max); // Output: 30
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

More Related Articles

JavaScript 2D Array – Two Dimensional Array in JavaScript JavaScript
Remove the Last Element of an Array in JavaScript JavaScript
JavaScript Array push() Method JavaScript
Remove Object from Array using JavaScript JavaScript
Add an Element at Specific Position in JavaScript JavaScript
How to Center Text in HTML? CSS

Leave a Reply Cancel reply

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

Archives

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

Categories

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

Recent Posts

  • How to Center Text in HTML?
  • Design a Simple HTML Page | First HTML Project
  • Best Way to Initialize an Empty Array in PHP
  • HTML Description Lists
  • HTML Ordered Lists

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme