DEV Community


Discussion on: Keeping Your Code Simple

maxschumacher profile image

In my understanding, using the .reduce() function is somewhat misplaced here. You're not looking to reduce all values in an array to one value, but rather look for one element that has certain properties.

Finding things in arrays (and in life) is easier when there is order in search space.

Here's another approach to finding the longest string in an array:

const longestString = arr => arr.sort((a, b) => b.length - a.length)[0];