There is a way that is a bit of a hack. You can use
switch(true)
:
var a = true;
var b = false;
switch(true){
case a && b:
alert("a && b");
break;
case b:
alert("b");
break;
case b && !a:
alert("b && !a");
break;
case a || b:
alert("a || b");
break;
case a:
alert("a");
break;
case a && !b:
alert("a && !b");
break;
default:
alert("default");
break;
}
when a=false and b=false, the alert is "default".
when a=false and b=true, the alert is "b".
when a=true and b=false, the alert is "a || b".
When a=true and b=true, the alert is "a && b".
The
break;
is like the
else
here. You can omit the
break;
so it acts like a list of
if
s instead of
else if
s
Hope that helps ^_^
Andy
PS: you can play with this example here:
switch(true) on jsfiffle.net[
^]