Search code examples
javascriptif-statementternary

JavaScript ternary operator and if/else statement


Can anyone explain to me what is the difference between these two statements and why the second one does not work and the first one does:

  1. if (finalWord.length > 140) return false; else return finalWord;

  2. (finalWord.length > 140) ? false : finalWord;


Solution

  • It looks, you miss the return statement.

    return finalWord.length > 140 ? false : finalWord;
    

    You could shorten it to

    return finalWord.length <= 140 && finalWord;