Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • 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 *

  • JavaScript Array Methods
  • JavaScript Array.from() Method
  • JavaScript Array isArray() Method
  • JavaScript Array at() Method
  • JavaScript Array every() Method
  • JavaScript Array filter() Method
  • JavaScript Array find() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array indexOf() Method
  • JavaScript Array join() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method
  • JavaScript Array map() Method
  • JavaScript Array pop() Method
  • JavaScript Array push() Method
  • JavaScript Array reduce() Method
  • JavaScript Array reduceRight() Method
  • JavaScript Array reverse() Method
  • JavaScript Array shift() Method
  • JavaScript Array slice() Method
  • JavaScript Array some() Method
  • JavaScript Array sort() Method
  • JavaScript Array splice() Method
  • JavaScript Array toLocaleString() Method
  • JavaScript Array toReversed() Method
  • JavaScript Array toSorted() Method
  • JavaScript Array toSpliced() Method
  • JavaScript Array toString() Method
  • JavaScript Array unshift() Method
  • JavaScript Array values() Method
  • JavaScript Array with() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme