douglascrockford/JSLint — Issue #108: Unexpected else after return
Author: roderickObrist
Created: 2012-03-06 00:56:09+00:00
State: open

Hi Doug,
I have a small snippet of code which I am getting js lint warnings which either I don't understand or may be unintentional behavior.

/*jslint sloppy: true*/
/*globals pingResult*/
(function () {
if (pingResult.rssi >= -75) {
return "ping-pass";
} else if (pingResult.rssi > -85) {
return "ping-half-pass";
} else {
return "ping-fail";
}
}());

I know it needs strict mode and pingResult is undefined, but the entire script satisfies these requirements, I added the options for your convenience.

I get this:

Error:
Problem at line 6 character 9: Unexpected 'else' after 'return'.
return "ping-pass";
Problem at line 8 character 9: Unexpected 'else' after 'return'.
return "ping-half-pass";

I know it can be silenced/avoided like this:

(function () {
var returnValue;
if (pingResult.rssi >= -75) {
returnValue = "ping-pass";
} else if (pingResult.rssi > -85) {
returnValue = "ping-half-pass";
} else {
returnValue = "ping-fail";
}
return returnValue;
}());

But is the second option better? because I cant see why?

(P.S thanks for JSLINT)