Javascript scope variable to switch case?

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 */

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)