

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the longest string in an array in JavaScript
We are required to write a JavaScript function that takes in an array of strings. Our function should iterate through the array and find and return the longest string from the array.
Our function should do this without changing the content of the input array.
Example
The code for this will be −
const arr = ["aaaa", "aa", "aa", "aaaaa", "acc", "aaaaaaaa"]; const findLargest = (arr = []) => { if(!arr?.length){ return ''; }; let res = ''; res = arr.reduce((acc, val) => { return acc.length >= val.length ? acc : val; }); return res; }; console.log(findLargest(arr));
Output
And the output in the console will be −
aaaaaaaa
- Related Questions & Answers
- Finding all the longest strings from an array in JavaScript
- Finding the longest word in a string in JavaScript
- Finding the longest substring uncommon in array in JavaScript
- Finding unique string in an array in JavaScript
- Get the longest and shortest string in an array JavaScript
- Finding the only unique string in an array using JavaScript
- Finding the longest valid parentheses JavaScript
- Finding the length of longest vowel substring in a string using JavaScript
- Finding longest consecutive joins in JavaScript
- Finding the rotation of an array in JavaScript
- Finding the mid of an array in JavaScript
- Finding the longest consecutive appearance of a character in another string using JavaScript
- Find longest string in array (excluding spaces) JavaScript
- Finding the longest "uncommon" sequence in JavaScript
- Finding the first redundant element in an array - JavaScript
Advertisements