using switch in a function

This shows an example using the switch to select multiple options in a function, you can return from the switch with break, or you can end the whole function with return.

Javascript

function using_switch($num) {
  $out = "";
  switch($num) {
    case 1: 
      $out = "one";
      break;
    case 2:
      $out = "two";
      break;
    case 3:
      $out = "three";
      break;
    default: // this is set when no case value is matched
      $out = "not found";
      break;
  }
  // do other things?
  return $out;
}
console.log(using_switch(2)); 
console.log(using_switch(1)); 
console.log(using_switch(6));

Demo

Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹