3
votes

Why does the following code trigger "Expected an assignment or function call and instead saw an expression." in JSHint? I thought this was the correct way to protect a block from executing if a particular variable or variables are not defined...

!function($) {
    "use strict";

    // jQuery-based code here
    $('.test').show();
}(window.jQuery);
1
This must be some error in JSHint or enforcement of coding styles. Since !function($){"use strict";}(window.jQuery); triggers the error and (function($){"use strict";}(window.jQuery)); not. And a function call has higher priority than the ! operator in javascript. - Prusse

1 Answers

2
votes

Replace the ! in front of function with parens around the entire call (e.g. (function(a,b){}("a","b"))) and all should be well. Which prompts me to ask why it is there in the first place since the function returns no value.

Any expression, as opposed to assignment, is flagged by JSHint with the message you quote.