Press "Enter" to skip to content

Javascript switch: Multiple conditions in single case

This tutorials briefs about how to check multiple conditions in single case in JavaScript switch statement.

Multiple case values in switch

By default,  case accepts only 1 value, the below function enables multiple values to a single case.

 var list = ['a','b','c','d'];
function checkMultipleValCase(varName){
function isInList(arg){
 for(var i=0;i<list.length;i++){
 if(list[i]==arg)return list[i];
 }
 }
var multipleCaseVals = isInList(varName);
 switch (varName)
 {
 case multipleCaseVals:
 alert(varName +' is in the case value list');
 break;
default:
alert(varName +' is not in the case value list and falls to default');
break;
}
}

Execute the function with the argument in it.

heckMultipleValCase('b');

change the argument value into ‘e’, it falls into the default condition.