How to find the longest word within the string in JavaScript ? - GeeksforGeeks

07 Apr, 2021
Given a string and the task is to find the largest word from the string using JavaScript.
Example:
Input: "This is a demo String find the largest word from it"
Output: "largest"

Input:  "Hello guys this is geeksforgeeks where students learn programming"
Output: "geeksforgeeks"
To achieve this we use the following approaches:
Using regex and for..loopUsing split and sort() methodUsing split and reduce() method
Using regex and for..loop
Using split and sort() method
Using split and reduce() method
Approach 1: Using regex and for...loop. In this approach, we use regex to split the string into an array of words by using the regex /[a-zA-Z0-9]+/gi and then using for loop iterate the array and search the largest string.
Example 1:
index.html
<script>// Javascript program to search largest word from a string  function longest(str){  // Split the string using regex  str = str.match(/[a-zA-Z0-9]+/gi);  // Creating a empty string to store largest word  let largest = "";      // Creating a for...loop to iterate over the array  for(let i = 0; i < str.length; i++){    // If the i'th item is greater than largest string     // then overwrite the largest string with the i'th value    if(str[i].length > largest.length){      largest = str[i]    }  }  return largest;}  console.log(longest("Hello guys this is geeksforgeeks where"+                    " students learn programming"))</script>
Output:
"geeksforgeeks"
Approach 2: By using the split() and sort() method. In this approach we split the string using the String.split() method and sort the array using the Array.sort() method. 
Example 2:
index.js
<script>function longest(str){  // Split the string into array  str = str.split(" ")  // Return the first sorted item of the Array  return str.sort((a, b) => b.length - a.length)[0]}    console.log(longest("Hello guys this is geeksforgeeks"+                    " where students learn programming"))</script>
Output:
"geeksforgeeks"
Approach 3 : Using split() and reduce() method. In this approach, we split the string using the String.split() method, and by using the reduce method we search the largest element of the array, which is your largest string.
index.js
<script>function longest(str){  // Split the string into array  str = str.split(" ");      // Get the index of largest item of the array  let index = str.reduce((acc, curr, i)=>{    if(curr.length > str[acc].length){      return i    }    return acc;  }, 0)      return str[index];}    console.log(longest("Hello guys this is geeksforgeeks"+                    " where students learn programming"))</script>
Output:
"geeksforgeeks"
JavaScript-Methods
JavaScript-Questions
javascript-string
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
How to calculate the number of days between two dates in javascript?
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript