Dito
Dito

Reputation: 995

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;

Upvotes: 3

Views: 69

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386520

It looks, you miss the return statement.

return finalWord.length > 140 ? false : finalWord;

You could shorten it to

return finalWord.length <= 140 && finalWord;

Upvotes: 4

Related Questions