Check if javascript is null in switch case

I wrote the code below.

if(result === ""){ show("Something went wrong!!"); } else if (result === "getID") { show("success"); } else { doSomething(); } 

How can I write this using the case switch statement in JavaScript. I am not sure how I can check for a null value in the condition of the switch condition.

Can someone help me?

+6
source share
2 answers

In this example, it does not matter if the result is null or "" , control will reach console.log("Something went wrong");

 switch (result) { case null: case "": console.log("Something went wrong"); break; case "getID": console.log("Success"); break; default: console.log("doSomething"); } 
+12
source
 switch (result) { case "" : document.write("Something went wrong!!<br>"); break; case "getID": document.write("success<br>"); break; default: dosomething(); } 
0
source

All Articles