34

I have the following line:

imageUrl && (data.imageUrl = imageUrl);

For this line, JSHint complains:

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

I understand the warning, but I want to disable it. I can’t find the way how to do it. Any idea?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Naor
  • 23,465
  • 48
  • 152
  • 268

2 Answers2

45

If it really is just that one line

imageUrl && (data.imageUrl = imageUrl); // jshint ignore:line

I am a fan of writing code so it doesn't produce warnings, though, even if this means taking special action to do so. So I'd prefer

if (imageUrl) data.imageUrl = imageUrl;
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
  • Know what? Took your advice! `if (imageURL) data.imageUrl = imageUrl` looks similar and better. Thanks! – Naor Dec 23 '14 at 16:14
  • What about `a || (a = [])`? Have any improvement for that? – Naor Dec 23 '14 at 16:19
  • `if (!a)`? `if (a.length != 0)`? Depends on what you're trying to do. Or `if (a) ; else a=[];`. Please don't do that one! (you'll get a different warning, in any case) – The Archetypal Paul Dec 23 '14 at 16:23
  • Thanks! Have any idea what about: `a = (b === c)`? I get `Expected an assignment or function call and instead saw an expression.` – Naor Dec 23 '14 at 16:54
  • OK, that's an odd one. I don't immediately see why it's complaining about that. On the other hand, jshint.com doesn't complain about it, so are you sure that's exactly what you used? No chance the first `=` was accidentally `==`? (which does result in your error) – The Archetypal Paul Dec 23 '14 at 16:55
  • And this comment chains is getting long. You should probably edit your question with all the statements/expressions you'd like to suppress warnings for – The Archetypal Paul Dec 23 '14 at 16:58
41

Include a /* jshint expr: true */ comment in your Javascript.

Reference: http://jshint.com/docs/options/#expr

Paul Roub
  • 36,322
  • 27
  • 84
  • 93