.reduce((maxWord, word) => { return word.length > maxWord.length ? word : maxWord; }, '')
function findLongestWord(string) { if (typeof string !== 'string' || string.trim().length === 0) { return ''; } return string.trim().split(/\s+/).reduce((maxWord, word) => { return word.length > maxWord.length ? word : maxWord; }, ''); } let longestWord = findLongestWord(" abc def ghiiii axiiii "); console.log(longestWord); // Output: "ghiiii"