var t = "TEST";
switch(t){
case !"TEST": /* <- does not work. Can you check if t does NOT contain a string? */
alert("t != TEST");
break;
}
Can you do this with a switch statement?
Yes, switch "[uses] the strict comparison, === ".
If you want pass any value in switch statement and then apply condition on that passing value and evaluate statement then you have to write switch statement under an function and pass parameter in that function and then pass true in switch expression like the below example.
Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.
you can also do this:
var str = "TEST";
switch (true) {
case (str !== "TEST") :
alert("str !== TEST");
break;
case (str === null) :
alert("str is null");
break;
default:
alert("default trigger");
}
You can use if. If you prefer switch, use a 'default' case and an if condition there like
default:
if(n == -1){
//Your code
}
break;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With