I “want” to use switch, but i can’t seem to get it to work when I use it in this fashion. Anyone know why?
var mystring='my name is johnny';
switch (!-1) {
case mystring.indexOf('hello'):
alert('hello');
break;
case mystring.indexOf('goodbye'):
alert('goodbye');
break;
case mystring.indexOf('johnny'):
alert('johnny');
break;
default:
alert('default');
}
it always alerts “default”, but you can see that I want it to alert “johnny”
Disclaimer: This switch is evil. Use if/else-if statements. but if you must use a switch it can be done as such:
Should work as you want it to.
.test.I would be tempted to refactor it further.