本文共 765 字,大约阅读时间需要 2 分钟。
The ternary operator is the only operator in JavaScript that works with 3 operands, and it’s a short way to express conditionals.
三元运算符是JavaScript中唯一可以使用3个操作数的运算符,这是表达条件的一种简短方法。
This is how it looks:
看起来是这样的:
? :
The condition <condition>
is evaluated as a boolean, and upon the result, the operator runs the first expression (if the condition is true) or the second.
条件<condition>
被评估为布尔值,并且根据结果,运算符将运行第一个表达式(如果条件为true)或第二个表达式。
This is an example: we check if running
equals to true, and if this is the case we call the stop()
function. Otherwise we call the run()
function:
这是一个例子:我们检查,如果running
等于真的,如果是这样的话,我们调用stop()
函数。 否则,我们调用run()
函数:
Example usage:
用法示例:
const running = true;(running === true) ? stop() : run()
翻译自:
转载地址:http://rvqgb.baihongyu.com/