Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript scope variable to switch case?

Tags:

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  }
like image 555
Philip Kirkbride Avatar asked Dec 06 '16 15:12

Philip Kirkbride


People also ask

Can we use variables in switch case JavaScript?

Your switch statement is invalid in JS at least. You can define variables inside the cases or outside switch.

Can we declare variables in switch case?

Declaring a variable inside a switch block is fine. Declaring after a case guard is not.

What is $scope in JavaScript?

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.

How do I scope a variable to a switch case?

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.

Why can't I use variables inside a switch statement in JavaScript?

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.

What is a switch case in JavaScript?

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.

What is the scope of a local variable in JavaScript?

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.


2 Answers

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 */ 
like image 80
Fatma Nabilla Avatar answered Sep 29 '22 21:09

Fatma Nabilla


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 (...) ... })(); 
like image 22
deceze Avatar answered Sep 29 '22 21:09

deceze