Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript switch break unreachable code detected

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.

like image 762
user2062455 Avatar asked Jul 05 '17 01:07

user2062455


People also ask

Why is my JavaScript code unreachable?

The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less return statement but including an expression directly after.

Is break required in switch JavaScript?

The break KeywordIt is not necessary to break the last case in a switch block. The block breaks (ends) there anyway. Note: If you omit the break statement, the next case will be executed even if the evaluation does not match the case.

What is unreachable code detected?

Detection of unreachable code is a form of control flow analysis to find code that can never be reached in any possible program state. In some languages (e.g. Java) some forms of unreachable code are explicitly disallowed.

Why is my code unreachable C++?

Unreachable code is a compile-time error in languages like C++, Java, and C or Unreachable code error occurs when the code can't be compiled; but why it is just a warning in C++ & C ? Warnings are a way that the compiler indicates that the particular mentioned thing might become an error in future.


1 Answers

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

like image 167
Dij Avatar answered Oct 05 '22 07:10

Dij