jshint throws a"Expected a 'break' statement before 'case'"

sakthisundar picture sakthisundar · Mar 14, 2014 · Viewed 15.6k times · Source

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 ?

Answer

Gerald Schneider picture Gerald Schneider · Mar 14, 2014

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
}