JavaScript三元运算符

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:

看起来是这样的:

<condition> ? <expression> : <expression>

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()

翻译自: https://flaviocopes.com/javascript-ternary-operator/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值