59

Hi I am having a trouble when my framework is using jshint to validate my javascript code. I have used switch-case without a break statement intentionally, but this portion of code is captured as an error when jshint checks. My code is something like below.

    switch (<no>){
    case 1:
        // does something
    case 2:
        //does something more
    default:
        // does something even more
   }

Error from 'jshint' is like Line 203 character 41: Expected a 'break' statement before 'case'. Any thoughts on how to avoid it ? or is it a bad practice to use switch cases in this scenario at all ?

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
  • What's the real world case? Why don't you need `break`? – elclanrs Mar 14 '14 at 07:04
  • When the case 2 hits , I need case 2 & all below should get executed. It is true for all the cases. Something of that sort. My logic works fine, but the validation fails. – sakthisundar Mar 14 '14 at 07:06
  • 13
    I often have switches where I don't really need `break` in every case, that's not too uncommon. – Gerald Schneider Mar 14 '14 at 07:06
  • possible duplicate of [switch fall through being ignored by JSHint](http://stackoverflow.com/questions/19015612/switch-fall-through-being-ignored-by-jshint) – falinsky Mar 14 '14 at 07:08
  • If you're just doing a fall-through it should be ok, but maybe there's a better alternative, posting your real code would help. A dictionary lookup is a common alternative. – elclanrs Mar 14 '14 at 07:08
  • The same thing happens when I have a rather complex statement with a `return` statement somewhere in the `switch` (e. g. a nested switch). I haven’t used `break` because it seemed redundant. – Sebastian Simon May 04 '15 at 05:29

2 Answers2

138

Copy & paste from the documentation:

Switch statements

By default JSHint warns when you omit break or return statements within switch statements:

[...]

If you really know what you're doing you can tell JSHint that you intended the case block to fall through by adding a /* falls through */ comment

So in your case:

switch (<no>) {
  case 1:
    // does something
    /* falls through */
  case 2:
    //does something more
    /* falls through */
  default:
    // does something even more
}
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • 37
    Thanks for just giving him the answer without all the back and forth grilling the guy about why he wants to do this :) – Dave Pile Feb 02 '15 at 04:04
  • 10
    +1. by design, switch statements support breaks, or no breaks, depending on the behavior you need. Otherwise, why would languages require you to add a "break" at all – Eric Rowell Jun 16 '16 at 04:17
-1

Exactly, breaks may be totally superfluous, like in this example

function mapX(x){
  switch (x){
    case 1:
      return A;
    case 2:
      return B;
    default:
      return C;
  }
}

In this case, if you would have had a break after return, JS Standard would throw a warning, namely Unreachable code.

Trying to conciliate jshint and JS Standard is tricky, but as stated, the solution would be

function mapX(x){
  switch (x){
    case 1:
      return A;
      /* falls through */
    case 2:
      return B;
      /* falls through */
    default:
      return C;
  }
}
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109