-2

In my JavaScript I want to have a function exit early if certain conditions are not met, like in the following example.

(function () {
  var something = false

  if (!something) return

  doMoreStuff()
}())

I'm ommitting semicolons out of preference and it works just fine (the above snippet works as expected).

JSHint keeps giving me the error Line breaking error 'return'. While this can be fixed by including a semicolon after the return statement, I'd rather keep semicolons out of my script.

Is there any option I can set to allow for an empty return at the end of a line without a semicolon?

4

2 回答 2

3

Is there any option I can set to allow for an empty return at the end of a line without a semicolon?

No, there is not. Here's the JSHint code for handling return statements:

stmt("return", function () {
    if (this.line === state.tokens.next.line) {
        //...   
    } else {
        nolinebreak(this); // always warn (Line breaking error)
    }
    // ...
})

If there is no semicolon or value following the return statement it will always enter the else condition and always warn.

于 2013-05-16T09:23:01.557 回答
1

Maybe you should use return null or return undefined.

于 2013-05-16T09:25:04.207 回答