9

I have the following code

   switch (attr.templateType) {

      case 'text': return tpl_default; break;
      case 'currency': return tpl_currency; break;
      case 'percentage': return tpl_percentage; break;
      case 'latlong': return tpl_latlong; break;
      case 'tel': return tpl_phone; break;
      case 'number': return tpl_number; break;
      case 'address': return tpl_address; break;
      case 'date': return tpl_date; break;
      case 'permissions': return tpl_permissions; break;
      case 'pagination': return tpl_pagination; break;
      case 'time': return tpl_time; break;
      case 'notEmpty': return tpl_notEmpty; break;

      default: return tpl_default; break;
    }

and JavaScript lint tells me "unreachable code detected" for ALL the breaks. If i take out the breaks, lint has no errors.

does anyone know why? The code works and with out any errors.

user2062455
  • 411
  • 5
  • 14

2 Answers2

22

why is break required after return? switch will return and break will never execute, that is why it is unreachable.

Dij
  • 9,761
  • 4
  • 18
  • 35
  • 1
    thanx, Dij and derp. I thought break is needed on a switch no matter what...but what you are saying makes sense. "no code will execute after return". – user2062455 Jul 05 '17 at 01:57
6

you don't need break because you are returning out of the function. No code will execute after the return

derp
  • 2,300
  • 13
  • 20