Can I omit the else in an inline javascript if statement?

后端 未结 7 1372
鱼传尺愫
鱼传尺愫 2021-02-07 02:50

I\'m trying to use this and it doesn\'t appear to be working. I\'m guessing it\'s just not an option, but want to confirm. Is this valid?

(if_it_is) ? thats_cool         


        
相关标签:
7条回答
  • 2021-02-07 03:30

    No, you can't. It's not an "inline if statement", it's the ternary operator, and that is, well… ternary.

    (if_it_is) ? thats_cool() : null;
    

    Since you do not seem to be interested in the return value of the operation, you could just write:

    if (if_it_is) thats_cool();
    

    though. That would also be better style than using a ternary.

    0 讨论(0)
提交回复
热议问题