Benefits of using the conditional ?: (ternary) operator

后端 未结 17 1206
孤街浪徒
孤街浪徒 2020-11-22 06:55

What are the benefits and drawbacks of the ?: operator as opposed to the standard if-else statement. The obvious ones being:

Conditional ?: Operator

17条回答
  •  温柔的废话
    2020-11-22 07:44

    If I'm setting a value and I know it will always be one line of code to do so, I typically use the ternary (conditional) operator. If there's a chance my code and logic will change in the future, I use an if/else as it's more clear to other programmers.

    Of further interest to you may be the ?? operator.

提交回复
热议问题