OneCompiler

[JavaScript] How to get longest string from an array?

I want to find the longest string (in terms of length) from an array, How can I do that in Javascript?

1 Answer

4 years ago by

You can use either of the below solutions to find the longest string present in an array

Method: 1

array.reduce((a, b) => a.length >= b.length ? a : b))

Method: 2

array.sort((a, b) => b.length - a.length)[0]

Try running the above solutions here : https://onecompiler.com/javascript/3xn6sah8c

4 years ago by Meera