DEV Community

Esther Adebayo
Esther Adebayo

Posted on

Three Ways to Find the Longest Word in a String using JavaScript

Being able to look for multiple ways to solve a problem is one of the reasons I enjoy being a developer.

In this article, I'm going to share with you 3 ways to find the longest word in a string using JavaScript. They are:

  1. The find() Method
  2. The sort() Method
  3. The for loop Method

The find() Method

function findLongestWord(str) {
  //split string into array
  const splStrArray = str.split(" ");

  //find the length of each item in the string
  const lengthofStringArray = splStrArray.map(item => item.length);

  //find the max length
  const maxLengthinArr = Math.max(...lengthofStringArray);

  //match the word with the length
  const result = splStrArray.find(item => item.length === maxLengthinArr);
  return result;
}
findLongestWord("I love my dogs a whole lot");
Enter fullscreen mode Exit fullscreen mode

This is one of the easiest methods because it helps me breakdown the solution into bits.

Explanation
The first thing is to split the string into an array. You need to do this in order to give each "word" or item in the array a numerical value, i.e, index.

After doing that, map over the array to get the exact length of each item in the array.

Once we get the exact length of each item, we only need to find the item with the maximum value. We do this using Math.max().

Lastly, we know that any item in the array with the longest length is the word we want. So, we match the item in the array with the string with the max length.

The sort() Method

function findLongestWord(str) {
  //split string into an array
  const splStrArray = str.split(" ");

  //order the length of words in the array from highest to lowest using the sort array method 
  const orderedArray = splStrArray.sort((a, b) => b.length - a.length)

  //pick out the longest which would be the first item in the array with index 0
  const longestWord2 = orderedArray[0]
  return longestWord2

}

findLongestWord("I love my dogs a whole lot")
Enter fullscreen mode Exit fullscreen mode

This sort() method is by far the fastest way I know of.

Explanation
Just like we did earlier, the first thing is to split the string into an array.

We need to sort by some comparison method between the first and second item in an array. In our case, the length of the second element, is compared to the length of the first element in the array.

This returns the array in a descending order and we pick the first word, with an index of 0.

The For loop method

function findLongestWord(str) {
  const splStrArray = str.split(' ');

  //initialize a variable to store the longest word
  let longestWord = "";
  for(let index = 0; index < splStrArray.length; index++){
    if(splStrArray[index].length > longestWord.length){
         longestWord = splStrArray[index];
     }
  }
 return longestWord
}
Enter fullscreen mode Exit fullscreen mode

Explanation
Split the word into an array immediately using split().

After doing this, initialize the longest word to an empty string. Then use the for loop to map over the items in the array.

The longest word returns as the word with the longest index.

I hope this was helpful to you, please comment or click the heart if this helped you

If you have more ways of solving this, be sure to share them below!

Top comments (0)