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

Fre*_*die 4 javascript

?var truth = true;
(truth) ? console.log('It is true') : throw new Error('It is not true');???????????????
Run Code Online (Sandbox Code Playgroud)

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

ant*_*oft 17

javascript区分语句和表达式.三元运算符只处理表达式; throw是一个声明.


LWC*_*ris 5

它确实有效,但问题在于“else”分支中的 throw 语句。

(truth) ? console.log('It is true') : (function(){throw 'It is not true'}());
Run Code Online (Sandbox Code Playgroud)