Skip to content

Latest commit

 

History

History
54 lines (43 loc) · 1.08 KB

change-if-else-to-switch.md

File metadata and controls

54 lines (43 loc) · 1.08 KB

How to Change an If-Else Statement to a Switch Statement


In JavaScript, you can substitute the if...else statement with a switch statement.

The function below is an if-else statement:

function chainToSwitch(val){
  let answer = "";
  
  if (val === "hip hip") {
    answer = "hooray!"
  } else if (val === 7) {
    answer = "Ate 9"
  } else if (val === "Michael") {
    answer = "Jackson"
  } else {
    answer = "Not this time!"
  }
  
  return answer;
}

The function below is a switch statement:

function switchedChain(val){
  let answer = "";
  
  switch (val) {
    case "hip hip":
      answer = "hooray!";
      break;
    case 7:
      answer = "Ate 9";
      break;
    case "Michael":
      answer = "Jackson"
      break;
    default :
      answer = "Not this time!"
      break;
  }
  
  return answer;
}

example link - if-else link - switch