Why does JSLint complain about "Unexpected" after "return"?

JSLint complains that the following (useless example) code is invalid:

(function (x) { "use strict"; if (x === 1) { return 1; } else if (x === 2) { return -1; } return 0; }(1)); 

Error: Problem with line character 4: Unexpected "else" after "return".

return 1;

Is this seriously suggesting that it is bad to use return statements inside an if / else structure?

He believes that this version is in order:

 (function (x) { "use strict"; var returnval = 0; if (x === 1) { returnval = 1; } else if (x === 2) { returnval = -1; } return returnval; }(1)); 
+52
javascript jslint
Feb 28 '12 at 11:02
source share
3 answers

It just tells you that else after return superfluous. Good:

 (function (x) { "use strict"; if (x === 1) { return 1; } if (x === 2) { return -1; } return 0; }(1)); 
+84
Feb 28 '12 at 11:07
source share

What I found w / jslint is that if you adhere to the rules - 50% are funny, but do not adversely affect your code. The other 50% (or so) will give you good benefits. So do it for the other 50%. This specific example makes you be explicit about the opposite of a condition or the like ... instead of letting it be implicit with else ... the same goes for if / else, which I mean.

+4
Aug 16 '12 at 10:11
source share

It is better to have a function always return something, as it adds consistency. It is known that JSLint is quite strict and infringes on the feelings of programmers. Can not help. Personally, I think version 1 is great.

+1
Aug 09 2018-12-12T00:
source share



All Articles