Switch case in JavaScript

Suppose that I want to “switch case” in JavaScript. I want to deal with the possbility that a == 1, a > 1, or a < 1. So I bang out the following code:

function foobaz(a){
        switch(a) {
            case 1:
                console.log("a is equal to 1");
            case a > 1:
                console.log("a is greater than 1");
            case a < 1:
                console.log("a is less than 1");
                console.log("it's really hard to compare a to 1")
    foobaz("The Queen of Hearts, she had some tarts!");

I’m expecting to get each of the four messages in sequence.
However, I actually get “a is equal to 1” and then “it’s really heard to compare a to 1” three times. Apparently I’m failing to properly define a > 1 and a < 1; how should I alter my code to achieve that effect?


I cleaned up your code.
Triple backticks go in their own lines.

1 Like

You can’t use a > 1, etc. in switch statements. They actually evaluate to true or false. Neither of which are equal to 2, -4, and "The Queen of Hearts, she had some tarts!", so you always hit default.

1 Like

I’ve got no way to use the non-equalities in Switch? Huh; okay, no use wasting more time figuring out how to do it, then. Thank you.