5

I'm trying use switch statement to perform a task. I have declared a variable using let in first case. But when i try to access it in second case it says "Uncaught ReferenceError: t is not defined"

x = "20";
switch (x) {
  case '10':
    let t = 15;
    break;
  case '20':
    console.log(t);
}

I get this error

Uncaught ReferenceError: t is not defined at :6:21

but if i try to declare same variable in second case then

x = "20";
switch (x) {
  case '10':
    let t = 15;
    break;
  case '20':
    let t = 150;
    console.log(t);
}

i get this error

Uncaught SyntaxError: Identifier 't' has already been declared

why is this happening? how to solve this issue without using var and without declaring variable with different name. Thanks in Advance.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
MNA
  • 287
  • 4
  • 14
  • 2
    Declare `let t;` above your switch statement. – Robby Cornelissen Dec 17 '18 at 07:20
  • you initialize t in case 10, so in case 20 it is not available (scoping) – mtizziani Dec 17 '18 at 07:21
  • 1
    So `case`s don't count as separate blocks, interesting, and that will make things difficult to handle when you also want to use modern syntax like `let` and `const`. Though you could declare the variable before the `switch` *once*, I'd recommend ditching `switch` entirely, if possible (maybe instead use an object indexed by `x`s, whose properties are the associated values) – CertainPerformance Dec 17 '18 at 07:22
  • "*why is this happening?*" - in the first case it should be pretty obvious: `t` was never initialised in the case `20`. – Bergi Dec 17 '18 at 07:29
  • 2
    It's because you're redeclaring a `let` statement *in the same function scope*. That will raise a Syntax error. The docs on [Redeclarations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Redeclarations) mention this exact case. If you want to have a new `let` statement in each case, then use the block syntax instead `case: { let a; }` instead of `case : let a;`. This way you'll be creating separate scopes. – mastef Dec 17 '18 at 07:32

1 Answers1

0

If you declare the variable in case '10' and you are trying to access the variable in case '20', you will get that error because you have not declared the variable in the scope of case '20'.

If you declare the variable multiple times in different cases then the compiler will understand that you are trying to declare the variable multiple times; so, it gives you that error.

This is what it should be. You declare it before the switch statement. Therefore, the variable is usable in every case beneath the switch.

x = "20";
    let t = 0;
    switch (x) {
        case '10':
        t = 15;
        break;
    case '20':
        t = 150;
        console.log(t);    
    }
holydragon
  • 6,158
  • 6
  • 39
  • 62
  • 4
    but this does not answer my question, that why this happening – MNA Dec 17 '18 at 07:22
  • @ImMNA could you explain how this puts `t` in the global scope? Are you trying to declare a variable at the block scope (`switch` statement)? – stealththeninja Dec 17 '18 at 07:24
  • 2
    i think this is the correct answer... – Syamsoul Azrien Dec 17 '18 at 07:24
  • @stealththeninja I'd assume that the `switch` statement is on the top level, in which case assigning to a `t` which was declared on the top level will effectively make `t` global. – CertainPerformance Dec 17 '18 at 07:25
  • @SyamsoulAzrien see the duplicate. Looks like Bergi already contributed a well-received answer, even though it’s not the accepted answer. – stealththeninja Dec 17 '18 at 07:34
  • or if you still don't want make the `t` as global variable, you can put @holydragon's code inside an `IIFE` (Immediately Invoked Function Expression)... more detail here about `IIFE`: https://developer.mozilla.org/en-US/docs/Glossary/IIFE – Syamsoul Azrien Dec 17 '18 at 07:34
  • 3
    Yes this will work but If you want to limit the scope of t for each case, then simply wrap it in blocks.like case variable:{ let t = ... } – Wajdan Ali Dec 17 '18 at 07:40