What is the JavaScript variable scope in a switch / case statment?

Posted by Todd Moses on Stack Overflow See other posts from Stack Overflow or by Todd Moses
Published on 2010-03-26T15:25:24Z Indexed on 2010/03/26 15:33 UTC
Read the original article Hit count: 271

Filed under:
|
|

While creating JavaScript with ASP.NET MVC I noticed several scope warnings and realized that I am missing something with understanding the variable scope inside the switch / case statement.

Warning: 'i' is already defined referring to case b and case c

My code looks similar to this:

switch(element) {
  case 'a':
   for(var i=0; i < count; i++){
    do something
   }
   break;

  case 'b':
   for(var i=0; i < count; i++){
    do something
   }
   break;

  case 'c':
   for(var i=0; i < count; i++){
    do something
   }
   break;
}

I thought scope ended with each break statement but it seems that scope does not end until the end of the switch/case. Is scope for the entire switch/case?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about asp.net-mvc