Member-only story

Exploring JavaScript’s Reduce Method: Simplifying Maximum and Minimum Value Searches

Beck Moulton
3 min readApr 17, 2024

--

About finding the maximum and minimum values in an array, I believe everyone can immediately think of the following method, which is a built-in method provided by JavaScript, simple and practical!

Math.max() // or
Math.min()

But today I’m going to share with you a method to use reduce to get the maximum and minimum values. But before that? I will first explain the reduce method to you. Some students may not be very familiar with this method, so I will explain how to implement finding the maximum and minimum values.

reduce

Syntax

In the syntax, we can see that the reduce function consists of a function and an initial value, detailed explanations can be found in the documentation.

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
  • total -- Required. The initial value, or the value returned after the computation.
  • currentValue -- Required. The current element.
  • currentIndex -- Optional. The index of the current element.
  • arr -- Optional. The array object to which the current element belongs.
  • initialValue -- Optional. The initial value passed to the function.

After understanding this function method, some of you may still not know how to use it, so let’s understand…

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

Responses (1)