Java Switch Statement - Is "or"/"and" possible?
There are quite a few answers like the above that give good alternatives to using this erroneous syntax in my example but I haven't read an explanation to why it doesn't work:
const switchExample = (val) => {
switch(val) {
case 'a' || 'b' || 'c':
return 'first 3'
break;
case 'd':
return 'fourth'
break;
default:
return 'default'
}
}
Invoking with 'b' or 'c' as a param will return the default. Whereas 'a' will return (as expected) 'first 3'. I was expecting that if 'b' was truthy, then the first case would evaluate to true and return 'first 3' but this ins't the case. Can somebody explain why?