Why do JSLint complain about & ldquo; Unexpected 'else' after 'return' &...
source link: https://www.codesd.com/item/why-do-jslint-complain-about-unexpected-else-after-return.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Why do JSLint complain about & ldquo; Unexpected 'else' after 'return' & rdquo;
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 at line 4 character 9: Unexpected 'else' after 'return'.
return 1;
Is it seriously suggesting that it's bad to use return statements inside an if/else structure?
It thinks this version is fine:
(function (x) {
"use strict";
var returnval = 0;
if (x === 1) {
returnval = 1;
} else if (x === 2) {
returnval = -1;
}
return returnval;
}(1));
It's just telling you that else
after return
is superfluous. The following is fine:
(function (x) {
"use strict";
if (x === 1) {
return 1;
}
if (x === 2) {
return -1;
}
return 0;
}(1));
Related Articles
Why Date :: Calc complaining about & ldquo; not a valid date & rdquo;
Why do perl complain about & ldquo; Too many arguments for open & rdquo; When I run it in a shell script?
Why does Perl complain about & ldquo; Could not stat on the file name containing newline & rdquo;?
JSON.parse complains of & ldquo; unexpected end of data & rdquo;
Why does the Pycharm inspector complain about & ldquo; d = {} & quot;
When I compile my program, why does he complain about the label & ldquo; at the end of the compound statement & rdquo;
Why does GHC complain about non-exhaustive models?
Why did Emacs complain about the year displayed variable-void in my .emacs file?
Why does PostgreSQL complain about the '0000-00-00 00:00:00' time stamp?
Why does the code continue to work even after 'return res.send ();'
Why do jQuery complain about a "left-handed assignment" & rdquo; ldquo ;?
Why do Perl's GD :: Graph complain about & ldquo; Invalid Data Set & rdquo;
Why XML :: RSS :: Parser from Perl complain about & ldquo; Discrepancy End Tags'?
Why this OCaml code receives & ldquo; Unexpectedly token & rdquo;
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK