Test for multiple cases in a switch, like an OR (||)

Andres picture Andres · Jun 28, 2011 · Viewed 151.1k times · Source

How would you use a switch case when you need to test for a or b in the same case?

switch (pageid) {
  case "listing-page" || "home-page":
    alert("hello");
    break;
  case "details-page":
    alert("goodbye");
    break;
}

Answer

kei picture kei · Jun 28, 2011

You can use fall-through:

switch (pageid)
{
    case "listing-page":
    case "home-page":
        alert("hello");
        break;
    case "details-page":
        alert("goodbye");
        break;
}