5

Switch cases are usually like

Monday: 
Tuesday: 
Wednesday: 
etc. 

I would like to use ranges.

from 1-12: 
from 13-19:
from 20-21:
from 22-30:

Is it possible? I'm using javascript/jquery by the way.

drummer
  • 1,211
  • 3
  • 16
  • 16
  • 1
    possible duplicate of [How can I use ranges in a switch case statement using JavaScript?](http://stackoverflow.com/questions/17145723/how-can-i-use-ranges-in-a-switch-case-statement-using-javascript) – Helen Sep 01 '13 at 09:09

4 Answers4

14

you could try abusing the switch fall through behaviour

var x = 5;
switch (x) {
    case  1: case 2: case 3: case 4: ...
        break;
    case 13: case 14: case 15: ...
        break;
    ...
}

which is very verbose

or you could try this

function checkRange(x, n, m) {
    if (x >= n && x <= m) { return x; }
    else { return !x; }
}

var x = 5;
switch (x) {
    case checkRange(x, 1, 12):
        //do something
        break;
    case checkRange(x, 13, 19):
    ...
}

this gets you the behaviour you would like. The reason i return !x in the else of checkRange is to prevent the problem of when you pass undefined into the switch statement. if your function returns undefined (as jdk's example does) and you pass undefined into the switch, then the first case will be executed. !x is guaranteed to not equal x under any test of equality, which is how the switch statement chooses which case to execute.

barkmadley
  • 5,207
  • 1
  • 28
  • 31
7

Late to the party, but upon searching for an answer to the same question, I came across this thread. Currently I actually use a switch, but a different way. For example:

switch(true) {
    case (x >= 1 && x <= 12):
        //do some stuff
        break;
    case (x >= 13 && x <= 19):
        //do some other stuff                
        break;
    default:
        //do default stuff
        break;
}

I find this a lot easier to read than a bunch of IF statements.

TheGremlyn
  • 323
  • 1
  • 3
  • 21
  • You can't do it like this in JavaScript, it won't evaluate the expressions. – zuallauz Nov 02 '11 at 02:55
  • Not sure what you mean - it works just fine in every test case I've thrown at it. – stephennmcdonald Jun 11 '12 at 14:56
  • @zuallauz Check the [specification](https://tc39.es/ecma262/#sec-switch-statement) — both _⟨expr1⟩_ and _⟨expr2⟩_ in `switch(` _⟨expr1⟩_ `){ case` _⟨expr2⟩_ `: break; }` are expressions which are all evaluated. This has been the case since ECMAScript 3rd edition. Prior to that, JavaScript didn’t even have a `switch` statement. – Sebastian Simon Mar 05 '21 at 23:17
1

You can make interesting kludges. For example, to test a number against a range using a JavaScript switch, a custom function can be written. Basically have the function test a give n value and return it if it's in range. Otherwise returned undefined or some other dummy value.

<script>

// Custom Checking Function..
function inRangeInclusive(start, end, value) {
    if (value <= end && value >= start)
        return value; // return given value
    return undefined; 
}

// CODE TO TEST FUNCTION
var num = 3;
switch(num) {
case undefined:
    //do something with this 'special' value returned by the inRangeInclusive(..) fn
    break;
case inRangeInclusive(1, 10, num):
    alert('in range');
    break;
default:
    alert('not in range');
    break;
}

</script>

This works in Google Chrome. I didn't test other browsers.

John K
  • 28,441
  • 31
  • 139
  • 229
  • @barkmadley: I just edited the code and added a case for the undefined value to explicitly handle it. There might be a better return value to use in the inRangeInclusive(..) function. Open to suggestions. Thanks. – John K Oct 26 '09 at 14:51
0

Nope, you need to use an if/else if series to do this. JavaScript isn't this fancy. (Not many languages are.)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578