3

I'm checking my JS files using JSHint to find mistakes.

I just got this warning :

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

(mandatEngagementExists)
    ? $('.mandat-type[value=1]').prop('checked', false).prop('disabled', true)
    : $('.mandat-type[value=1]').prop('disabled', false);

mandatEngagementExists is a boolean. All my JS is in strict mode.

This piece of code is working so I wanted to know if JSHint was a bit too strict or if I should switch to a classic if/else.

FLX
  • 2,626
  • 4
  • 26
  • 57
  • See [jslinterror](https://jslinterrors.com/expected-an-assignment-or-function-call) Description. The statement think that the value returned from function call should be catched in a variable, so it says `Expected assignment` – Tushar Oct 07 '15 at 14:20

2 Answers2

3

use boolean directly inside the .prop("property", boolean)

$(el).prop('checked', myBoolean);

Using return

function myFn() {
    /* Code here */
    return myBoolean ? $(el).doThis() : $(el).doThat();
    /* !!!!!!!!! >>> Cannot have more code here since `return` was used */
}

Using an IIFE

function myFn() {
     /* Code here */
    (function(){
       return myBoolean? $(el).doThis() : $(el).doThat();
    }());
     /* Code here */
}

Using var

var someVar;
function myFn() {
    /* Code here */
    someVar = myBoolean? $(el).doThis() : $(el).doThat();
    /* Code here */
}

Using if else

function myFn() {
    /* Code here */
    if(myBoolean) {
        $(el).doThis();
    } else {
        $(el).doThat();
    }
    /* Code here */
}
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • This would exit the current function – haim770 Oct 07 '15 at 14:16
  • @haim770 no if you set it at function end or wrap it inside another function like an IIFE – Roko C. Buljan Oct 07 '15 at 14:17
  • I was just about to mention IIFE as an option. But I think the whole point here is to save some key strokes (by not using `if {} else {}`), if you're already going with an IIFE, you don't gain anything, only more code. – haim770 Oct 07 '15 at 14:19
  • Thanks for this complete answer. I'll just stay with my code, it's the shortest – FLX Oct 07 '15 at 14:37
  • @FLX you're welcome. PS, the ***shortest possible*** is using `$(el).prop('checked', myBoolean);` as mentioned – Roko C. Buljan Oct 07 '15 at 14:37
  • In case of "prop" yes, but I'm also using this trick to switch between functions so it's not always possible – FLX Oct 07 '15 at 15:00
  • @FLX than you might be interested on this answer of mine: http://stackoverflow.com/a/21520499/383904 – Roko C. Buljan Oct 07 '15 at 15:10
2

JSHint considers ternary operators called ignoring the return value of the called functions as bad practice.

See Why does JSHint dislike ternaries for method calls on objects?

you can ignore the warning, set a fake variable

var temp=(mandatEngagementExists)
    ? $('.mandat-type[value=1]').prop('checked', false).prop('disabled', true)
    : $('.mandat-type[value=1]').prop('disabled', false);

or use this

/*jshint expr:true */
Community
  • 1
  • 1
valepu
  • 3,136
  • 7
  • 36
  • 67
  • 1
    Good answer. Personally I'd just switch to an `if/else` – makes the intention a whole lot clearer. Code is read far more often than it is written, after all. – Shai Oct 07 '15 at 14:24
  • Thanks. This jshint expression will be useful. – FLX Oct 07 '15 at 14:37