Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 972
Todd Moses Avatar asked Mar 26 '10 15:03

Todd Moses


People also ask

What is the scope variable in JavaScript?

The scope of a variable is the region of your program in which it is defined. JavaScript variables have only two scopes. Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code.

What is a switch case statement in JavaScript?

The switch statement executes a block of code depending on different cases. The switch statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions. Use switch to select one of many blocks of code to be executed.

What variables are used in a switch statement?

The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums. You can have any number of case statements within a switch.

Can we declare variable in switch case JavaScript?

Variables declared anywhere within the switch statement is locally scoped to the switch statement. This shows that the case statements themselves are not blocks, variables declared within them are hoisted to the switch statement block.


2 Answers

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:

switch(someVar) {
    case 1:
        int a;
        break;
    case 2:
        int a;        //'a' is already defined
        break;
}
like image 137
SLaks Avatar answered Oct 18 '22 06:10

SLaks


Is scope for the entire switch/case?

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.)

Warning: 'i' is already defined

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 add var i before the switch and remove var from all the for​s. But now those blocks don't stand alone, and a quick cut-and-paste (for, say, refactoring the switch into separate function​s) leaves you with loops referencing an i that hasn't been declared var. 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.

like image 38
bobince Avatar answered Oct 18 '22 06:10

bobince