Find the Longest String in an Array using JavaScript

Given an array of strings. The task is to find the longest string in the given array.

For example, if we have an array ["ab", "a", "bcde", "abd"], then the output should be "bcde" as it is the longest string among all the strings.

To solve this problem we can use the following approach:

  • Assume that the first string of the the array i.e. arr[0] is the longest string. Store its value in a variable eg. longestStr = arr[0].
  • Run a for loop on the array to access each string.
  • Check if the length of the current string is more than the longest string so far. If yes, make the current string as the longest string by storing its value in the longestStr variable.
  • If the length of the current string is less than the longest string so far, do nothing.
  • At last, print the output

In the following example, we have implemented the above approach to find the longest string:

// Array of strings
const arr = ["ab", "a", "bcde", "abd"];

// Assume first string is the longest
let longestStr = arr[0];

for(let i = 1; i < arr.length; i++){
    
    // Check if the current string is the longest 
    if(arr[i].length > longestStr.length){
        longestStr = arr[i];  
    }
    
}

console.log(longestStr);
// Output: "bcde"

Find Longest String using forEach() Method

The Array.forEach() method is an alternative to the regular for loop. It calls a callback function which one by one runs on each array item.

For eg., if we have an array of 5 numbers, it will one by one call the callback function on each number.

const arr = [1, 2, 3, 4, 5];

arr.forEach((num)=>{
    // print each number
    console.log(num);
});

// Output: 
// 1
// 2
// 3
// 4
// 5

As the callback function provided in the forEach() method is called for each array item one by one, it means we can also use it to find the longest string in the array.

Example:

// Array of strings
const arr = ["ab", "a", "bcde", "abd"];

// Assume first string is the longest
let longestStr = arr[0];

arr.forEach(currStr=>{
    
   // Check if the current string is the longest 
    if(currStr.length > longestStr.length){
        longestStr = currStr;  
    }
    
});

console.log(longestStr);
// Output: "bcde"

Conclusion

In this article, we learned two ways to find the longest string in an array using JavaScript.

The first approach to find the longest string is to assume the first string as the longest string and use a regular for loop to compare it with the remaining strings. If the longest string so far is not longer than the current string, make the current string as the longest. Continue this until the last string of the array.

The second approach is almost similar to the first approach. The only difference is that we used a forEach() method instead of the regular for loop.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts