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?

James Wiseman
  • 29,946
  • 17
  • 95
  • 158

1 Answers1

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

ic3b3rg
  • 14,629
  • 4
  • 30
  • 53
Raynos
  • 166,823
  • 56
  • 351
  • 396
  • 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 Jun 06 '11 at 07: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. – James Wiseman Jun 06 '11 at 07: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 Jun 06 '11 at 08:08
  • Is there now? Interesting. Thanks for your response :-) – James Wiseman Jun 06 '11 at 08: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 Dec 27 '13 at 14:14