【问题标题】:Javascript ternary boolean shorthandJavascript 三元布尔简写
【发布时间】:2019-11-10 17:10:53
【问题描述】:

以下 JavaScript 布尔三元表达式是否有简写语法:

var foo = (expression) ? true : false

【问题讨论】:

标签: javascript conditional-statements ternary-operator


【解决方案1】:

当然,您只想将表达式转换为布尔值:

var foo = Boolean(expression);

the same thing 缩写为double not operators

var foo = !! expression;

【讨论】:

  • 您能解释一下第二种解决方案吗?
  • @Noob 查看 Amy 评论中的帖子链接。但这是简单的逻辑。
  • 第二种解决方案正是我想要的。
猜你喜欢
  • 2017-02-18
  • 2021-05-24
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 2012-12-08
  • 2020-08-15
  • 1970-01-01
相关资源
最近更新 更多