I get a syntax error with
false ? {var x = 1;} : {var x = 2;}
But when just use expressions for the last two operands, I don’t run into any problems.
Do I have to resort to conditionals?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To explicitly answer the question, no, the 2nd and 3rd operands must be expressions, and cannot be statements. Here’s the MDN docs: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/Conditional_Operator
Note the syntax: condition ? expr1 : expr2
Edit: just an interesting side-note is that since function calls are expressions and you can pass contexts around, you can kind of accomplish what you were trying to do. It’s horrible. Don’t do it. But you can…