Searching for a particular element in a JavaScript array is often carried out using a typical iteration. In some cases, forEach and some can be used as well. What is often overlooked is the potential use of Array.prototype.reduce to perform such an operation.
Let’s take a look at the following problem: find the longest string in an array of strings. An imperative solution looks something like the following code (usingforEach may simplify the loop but the idea remains the same): ...
A version which relies on reduce is a single statement: ...