In C you can scope a variable to a switch case, like this.
With javascript I get unexpected token using the following:
const i = 1 switch (i) { // variables scoped to switch var s var x = 2342 case 0: s = 1 + x break case 1: s = 'b' break }
Is there another way to do this or should I just declare my variables outside the switch?
EDIT:
This is a workaround I considered but it didn't end up working. The reason being that each case has its own scope.
const i = 1 switch (i) { case i: // variables scoped to switch var s var x = 2342 case 0: s = 1 + x break case 1: s = 'b' break }
Your switch statement is invalid in JS at least. You can define variables inside the cases or outside switch.
Declaring a variable inside a switch block is fine. Declaring after a case guard is not.
Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. The two types of scope are local and global: Global variables are those declared outside of a block. Local variables are those declared inside of a block.
In C you can scope a variable to a switch case, like this. const i = 1 switch (i) { // variables scoped to switch var s var x = 2342 case 0: s = 1 + x break case 1: s = 'b' break } Is there another way to do this or should I just declare my variables outside the switch? This is a workaround I considered but it didn't end up working.
The reason being that each case has its own scope. const i = 1 switch (i) { case i: // variables scoped to switch var s var x = 2342 case 0: s = 1 + x break case 1: s = 'b' break } Your switch statement is invalid in JS at least. You can define variables inside the cases or outside switch.
Introduction to the JavaScript switch case statement The switch statement is a flow-control statement that is similar to the if else statement. You use the switch statement to control the complex conditional operations.
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.
some alternative:
/* curly braces inside the case */ const i = 1 switch (i) { case 0: { let x = 2342; let s = 1 + x; console.log(x+' & '+s+' from inside'); } break; case 1: { let x = 2342; let s = 'b'; console.log(x+' & '+s+' from inside'); /* 2342 & b from inside */ } break; } console.log(x+' & '+s+' from outside'); /* Uncaught ReferenceError */
or
/* curly braces outside the switch */ const i = 1 { let x = 2342; let s; switch (i) { case 0: s = 1 + x; break; case 1: s = 'b'; break; } console.log(x+' & '+s+' from inside'); /* 2342 & b from inside */ } console.log(x+' & '+s+' from outside'); /* Uncaught ReferenceError */
Since var
creates variables at function scope anyway, using it is pretty pointless. For this to work at a granularity below function scopes you'll have to use let
and a browser/compiler which supports it, and then introduce a new block which you can scope things to (within switch
it's simply invalid syntax):
if (true) { let s; switch (i) { ... } }
This scopes s
to the if
block, which for all intents and purposes is identical to the "switch
scope" here.
If you cannot support let
, you'll need to use an IIFE:
(function () { var s; switch (...) ... })();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With