if (stringVarName == "Word") doSomething(1); if (stringVarName == "Foo") doSomething(2); if (stringVarName == "Bar") doSomething(3); if (stringVarName == "Qwerty") doSomething(4);
switch (stringVarName) {
case "Word":
doSomething(1);
break;
case "Foo":
doSomething(2);
break;
case "Bar":
doSomething(3);
break;
case "Qwerty":
doSomething(4);
break;
default:
break;
}
function test(str) {
switch (true) {
case /word/.test(str):
doSomething(1);
break;
case /foo/.test(str):
doSomething(2);
break;
case /bar/.test(str):
doSomething(3);
break;
case /qwerty/.test(str):
doSomething(4);
break;
}
}