Home

Switch - like Select Case in msAccess

It is basically an easier way of using multiple IF( ) statements.
So instead of saying:
if(condition) {
   //Some Code
} else if(condition) {
   //Some Code
   }
}
etc...etc

Switch

This is javaScript's version of Select Case in MsAccess

switch(myFruit) {
   case "Banana":
      text = "Banana is good";
      break;
   case "Orange":
      text = "I suppose oranges are okay";
      break;
   default:
      text = "Unknown fruit ???";
}


This checks the value of myFruit and if it matches what is in one of the case statements it will run the relevant code.

NOTE...
On the end of EVERY case statement there must be a colon (:)
AFTER the code for each case statement there must be a break; statement otherwise it will just carry on to the code in the next case statement.
AT THE END after the LAST case statement there should be a default: statement in order to catch anything that does not match any of the above case statements.

ALSO NOTE...
As javaScript is CASE SENSITIVE things like "Banana" and "BANANA" will not match and it will not run the code you may expect.
Therefore I think it best to make sure and run it like this...

switch(myFruit.toLowerCase()) {
   case "banana":
      text = "Banana is good";
      break;
   case "orange":
      text = "I suppose oranges are okay";
      break;
   default:
      text = "Unknown fruit ???";
}


Using the .toLowerCase() option will make sure that it checks everything in lowercase
(or .toUpperCase() if you prefer)

For example...

I am using various upper/lower case in my dropdown box and checking just for lowercase in my code.

Select One:

Apples taste apple-y

Top