三元运算符react
The ternary operator "?:" earns its name because it's the only operator to take three operands. It is a conditional operator that provides a shorter syntax for the if..then..else statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third operand is returned:
三元运算符“?:”之所以得名,是因为它是唯一接受三个操作数的运算符。 它是一个条件运算符 ,可为if..then..else语句提供较短的语法。 第一个操作数是布尔表达式; 如果表达式为true,则返回第二个操作数的值,否则返回第三个操作数的值:
boolean expression?value1:value2
例子: ( Examples: )
The following if..then..else statement:
以下if..then..else语句:
boolean isHappy = true;String mood = "";if (isHappy == true){mood = "I'm Happy!";}else{mood = "I'm Sad!";}
can be reduced to one line using the ternary operator:
可以使用三元运算符减少到一行:
boolean isHappy = true;String mood = (isHappy == true)?"I'm Happy!":"I'm Sad!";
Generally the code is easier to read when the if..then..else statement is written in full but sometimes the ternary operator can be a handy syntax shortcut.
通常,如果完全编写if..then..else语句,则代码更易于阅读,但有时三元运算符可以是方便的语法快捷方式。
翻译自: /ternary-operator-2034302
三元运算符react
如果觉得《三元运算符react_三元运算符》对你有帮助,请点赞、收藏,并留下你的观点哦!