25

I recently received a comment on one of my blog posts about JSLint asking why JSLint threw an error with the following:

s === "test" ? MyFunc() : MyFunc2();

The error generated was:

"Expected an assignment or function call and instead saw an expression."

Clearly JSLint is expecting an assignment here, somthing more like:

var y = (s === "test") ? MyFunc() : MyFunc2();

But, I don't really see the problem with the first example. Is it really the case that ternary operators should only be used for assignments?

I couldn't see anything on JSLint.com, nor was there anything apparent in the book JavaScript: The Good Parts. And, the same error is also reported in the community fork JSHint.

Anyone?

1 Answer 1

35

It's an expression. It's equivalent to writing

0 === 1;

You're writing an expression that has immediate side effects and that's considered bad.

Generally expressions are useless statements that have no side effect. It's considered better form to simply do

if (s === "test") {
  MyFunc();
} else {
  MyFunc2();
}

Apart from that it's perfectly solid syntax. I personally do agree that writing a terse ternary as an alternative to an if is bad and you're better off only using it for assignment.

Other short hand expression that have been (ab)used for terse-ness

someCondition && doMagic(magic);
someCondition || doMagic(magic);

Again these are considered bad form if there used only as expressions because using these just obscures logic away and make it harder to maintain code.

JSHint has an option expr for this. See ticket

Running:

/*jshint
  expr: true
*/

var s, MyFunc, MyFunc2;
s === "test" ? MyFunc() : MyFunc2();
0 === 1;

Will pass

5
  • 1
    +1 for an answer from the mighty Raynos. I must admit, I sometimes use shorthand expressions rather than if statements. Rarely ternary though, usually something like match && doSomethingWith(match). But that's just because I'm lazy :P In general, I do think there are a couple of things JSLint complains about that you can ignore when personal preference comes into play.
    – Andy E
    Commented Jun 6, 2011 at 7:43
  • 1
    @Andy E: I realise there are parts of JSLint where personal preference does come into play, but was interested to see the same message reprted by JSHint as well. It's always good to know, though. Commented Jun 6, 2011 at 7:50
  • 1
    @JamesWiseman JSHint allows you a lot of customization. There's a expr option to suppress these warnings. In my opinion simple ||, && or ? are fine to use but I can see why they could be bad.
    – Raynos
    Commented Jun 6, 2011 at 8:08
  • Is there now? Interesting. Thanks for your response :-) Commented Jun 6, 2011 at 8:10
  • 1
    @Raynos: Your writing an expression that has immediate side effects and thats considered bad. Could you clarify that statement? What are immediate side effect? Good answer nevertheless!
    – kumarharsh
    Commented Dec 27, 2013 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.