为什么这个三元运算符在 JS 中无效


​var truth = true;
(truth) ? console.log('It is true') : throw new Error('It is not true');​​​​​​​​​​​​​​​

三元运算符是否只接受特定类型的对象?

javascript 区分语句和表达式。 三元运算符仅处理表达式;投掷是一个声明。

它确实有效,但问题是"else"分支中的 throw 语句。

(truth) ? console.log('It is true') : (function(){throw 'It is not true'}());

与所有其他运算符一样,条件运算符只能与表达式一起使用。

throw x;是一个陈述,而不是一个表达。

最新更新