Sometimes, we want to find the longest string in an array with JavaScript.
In this article, we’ll look at how to find the longest string in an array with JavaScript.
How to find the longest string in an array with JavaScript?
To find the longest string in an array with JavaScript, we can use the array map
and Math.max
methods.
For instance, we write
const longest = Math.max(...arr.map((el) => el.length));
'``
to call `arr.map` with a callback that returns the `length` of each string `el` and return the lengths in an array.
Then we call `Math.max` with the array entries spread into it as arguments.
This will then return the length of the longest string in `arr`.
### Conclusion
To find the longest string in an array with JavaScript, we can use the array `map` and `Math.max` methods.