-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?

Sacha
  • 2,813
  • 3
  • 22
  • 27
  • `if (!something) { return }` – Fabrício Matté May 16 '13 at 09:25
  • @alex23 This is not the same. My snippet returns `undefined`, yours returns `false`. @FabrícioMatté This is similar to adding a semicolon at the end. While it is correct, it's not what I'm trying to achieve. – Sacha May 16 '13 at 09:28
  • JavaScript is a functional language and functions in FP gotta return a value. AFAIS you're trying to emulate a procedural language that just ...returns. But that's not JavaScript's cup of tea. For the same reason, I'd stay out of `return undefined` altogether: `return false` or `return null` are much more meaningful. – Marco Faustinelli Apr 16 '17 at 06:41

2 Answers2

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.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
1

Maybe you should use return null or return undefined.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127