If without else ternary operator

前端 未结 10 721
太阳男子
太阳男子 2020-12-05 17:04

So far from I have been searching through the net, the statement always have if and else condition such as a ? b : c. I would like to know whether the if<

10条回答
  •  长情又很酷
    2020-12-05 17:25

    Well in JavaScript you can simply do:

    expression ? doAction() : undefined
    

    since that's what's literally actually happening in a real if statement, the else clause is simply undefined. I image you can do pretty much the same thing in (almost?) any programming language, for the else clause just put a null-type variable that doesn't return a value, it shouldn't cause any compile errors.

提交回复
热议问题