Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this ternary operator invalid in JS

Tags:

javascript

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

Do ternary operators only accept specific types of objects?

like image 673
Freddie Avatar asked Nov 21 '12 22:11

Freddie


People also ask

Can we use ternary operator in JavaScript?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Why ternary operator should not be used?

Except in very simple cases, you should discourage the use of nested ternary operators. It makes the code harder to read because, indirectly, your eyes scan the code vertically.

Which of the following is invalid operator in JavaScript?

The correct answer to this question “Which of the following is not considered a JavaScript operator” is option (b). this. Other than “this” all the other options are the JavaScript operators.

Which is the correct option for ternary operator?

?: = Question Mark Colon is also called C Ternary Operator.


1 Answers

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

like image 175
antlersoft Avatar answered Oct 27 '22 00:10

antlersoft