mercredi 31 janvier 2018

Switch statement not working properly - Javascript

Switch statement code:

var data = "manager";

 switch (data) {
  case "manager" :
  function test(){
     console.log('manager');
  }
  test();
  break; 

  case "worker" :
  function test(){
    console.log('worker');
  }
  test();
  break;

  default:
   console.log('default');
} //output showing as 'worker' but expected output  // 'manager'

but if i try this using if conditions,it will worked correctly.

If statement code:

var data = "manager";

 if (data == "manager") {
  function test(){
     console.log('manager');
  }
  test();
 } else if(data == "worker"){
   function test(){
    console.log('worker');
  }
  test();
 } else {
      console.log('default');
 } // got correct output as 'manager'

What's wrong with switch statement? is switch working as async?

Aucun commentaire:

Enregistrer un commentaire