"Error & Solution _ if(Ternary Operator), Evaluating true and false"

GaramYoonGaramYoon
1 min read

<Error code>

    const onClickButton = () => {
    if(!word || (newWord.length === 3 || word[word.length - 1] === newWord[0])){
        // ...code
    }
};

In this function, the first condition checks for newWord.length === 3. However, even if newWord.length !== 3, the if executes because the other condition is true.

<Debugging & Solution>

Update the if condition to ensure newWord.length === 3 must be true:

if (newWord.length === 3 && (!word || word[word.length - 1] === newWord[0])) {
    // ...code
}

<Lesson Learned>

Always verify if each logical condition matches the intended logic.

0
Subscribe to my newsletter

Read articles from GaramYoon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

GaramYoon
GaramYoon