Conditional Operators in Javascript

前端 未结 5 497
长发绾君心
长发绾君心 2021-02-07 06:22

Is it ok to use conditional operators like a statement like so?

(x == y) ? alert(\"yo!\") : alert(\"meh!\");

Or is it more correct to use it to

5条回答
  •  臣服心动
    2021-02-07 06:31

    It is entirely up to you, you can do it either way. You just have to ask yourself, though, does this style follow company guidelines, and how readable do you want this code to be?

    Using if statements is way more readable.

    Personally, I only use the ternary operator for simple and quick true/false conditions--where doing so makes sense--or where I need something "inline".

提交回复
热议问题