How to Find the Longest Word in a String in Javascript

In this tutorial, you will learn how to find the longest word in a string in javascript. A sentence consists of multiple words and the length of those words vary. There could be multiple long words in a string and I assume you only need any one of them.

Finding the longest word in a string is not straightforward since there is no built-in method to get it. As a newbie, it could be a bit tricky for you, but as a web developer, you should be aware of how to do so.

There are numerous ways to find the longest word in a string, but I will show you one of the easiest methods.  We will create a custom method that will take a string as a parameter and will return the longest word in that string.  Feel free to customize this method to fit your needs.

In the following example, we have one global string. We will pass it to our custom method which will return the longest word in that string. Later, we will log that word in the console window.  Please have a look over the code example and the steps given below.

HTML & CSS

  • We do not have any element in the HTML file because it is not required for this tutorial.
  • We have only included our javascript file script.js with a script tag at the bottom. The javascript code will run as soon as the page is loaded in the browser.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
    <script src="script.js"></script>
</body>
</html>

Javascript

  • We have a global variable myString and it holds a string.
  • We have created a custom method findLongestWord() which takes a string as a parameter.
  • In the findLongestWord() method, we are using the split() method to get rid of spaces in the string. This method returns an array of words.
  • A string can have special characters and in our case, we have multiple dots. We are using the map() method to loop through each word. In the anonymous function, we are using replace() method and passing it a regex string to get rid of dots in each word.
  • We are calling reduce() method and passing it an anonymous function as a parameter. In this function, we are simply comparing the length of the previous word with the length of the current word. At the end of execution, we will have the longest word in the string. We are storing it in the longest variable and returning it.
  • We are calling the findLongestWord() method and passing it our global string as a parameter. This method will return the longest word in that string.
  • We are storing the returned value in the result variable and logging it in the console window by using the console.log() method.
const myString = `A sentence is a textual unit consisting..................... of one or more words that are grammatically linked`;


function findLongestWord(msg){
    msg = msg.split(' ');
    msg = msg.map(word => word.replace(/\./g, ''));    
    let longest = msg.reduce((current, previous)=>{
        return current.length > previous.length ? current : previous;
    }, "");
    return longest;
}

const result = findLongestWord(myString);

console.log(result);