Why is this ternary operator invalid in JS

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

Do ternary operators use only certain types of objects?

+4
source share
3 answers

javascript distinguishes between expressions and expressions. The ternary operator processes only expressions; throw is a statement.

+17
source

It works, but the problem is with the throw expression in the else branch.

Using

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

The conditional operator, like all other operators, can only be used with expressions.

throw x; is an expression, not an expression.

+2
source

Source: https://habr.com/ru/post/1447613/


All Articles