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?
Javascript does not use block scope.
Therefore, all local variables are in scope throughout the entire function in which they were declared.
However, in your particular case, there is no C-like language (that I know of) in which each
case
statement forms an independent scope.For example, the following C# code will not compile:
No, it's for the entire containing function, or global scope if you're outside a function.
(There are a obscure few cases in which JavaScript introduces extra scope, but that's about it.)
I don't really agree with this being a warning. I would prefer to leave the code as it is, with the blocks' independent uses of the variable
i
.What it wants you to do is remove the
var
from all but the first declaration, or maybe addvar i
before the switch and removevar
from all thefor
s. But now those blocks don't stand alone, and a quick cut-and-paste (for, say, refactoring theswitch
into separatefunction
s) leaves you with loops referencing ani
that hasn't been declaredvar
. This is an accidental global, and it's a horrible JS trap which can be a real pain to debug.JSLint makes the same complaint. I would typically ignore it. It does no harm to declare a variable
var
twice in one block.Even if javascript had block scope, there is the fall-through feature which kinda disqualifies the notion of having a scope per
case
...