javascript switch break unreachable code detected
Asked Answered
G

2

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.

Goldarn answered 5/7, 2017 at 1:46 Comment(1)
Who is it returning to?Siccative
D
22

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

Decameter answered 5/7, 2017 at 1:49 Comment(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".Goldarn
I
6

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

Israelitish answered 5/7, 2017 at 1:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.