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

Updated on: 24-Nov-2020

454 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements