JavaScript - Return the length of the longest word in a sentence

Question - Return the length of the longest word in the provided sentence. Your response should be a number.

Solution Basic approach

console = { 
    log: print,
    warn: print,
    error: print
function findLongestWordLength(str) {
    //split the string to make it an array
    let words = str.split(" ");
    //declare 0 to compare with the every word length
    let maxLength = 0;
    //loop through the given string
    for (let i = 0; i < words.length; i++) {
        //compare with each stored value on the declared variable
        if (words[i].length > maxLength) {
        //if condition is true we get a new maxLength to compare with next words[i]
            maxLength = words[i].length;
    return maxLength;
console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));



download android app

Popular javascript Examples is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube