Syntax error when trying to test an empty string

advertisements

In Javascript, I am trying to do a simple task of testing and returning the longest of two words. If the words are empty then the return should say "empty string". I keep getting a syntax error when testing for an empty string with the or elseif statement. I am using (!word1) and (!word2) because my understanding is in Javascript it is a boolean statement and should be false. Please tell me where I am going wrong:

function longest(word1, word2) {

if (word1.length >= word2.length) {

    return (word1);

} else {

    return (word2);

} else if (!word1) || (!word2) {

    return "an empty string";

}

}

console.log(longest('hi'));


if conditions should be surrounded by exactly 1 pair of parentheses.

Change else if (!word1) || (!word2) to else if (!word1 || !word2).