简体   繁体   中英

JavaScript ternary operator and if/else statement

任何人都可以向我解释这两个陈述之间有什么区别,以及为什么第二个陈述不起作用,第一个陈述不起作用:

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

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

It looks, you miss the return statement .

return finalWord.length > 140 ? false : finalWord;

You could shorten it to

return finalWord.length <= 140 && finalWord;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM