Is there a way in Javascript to compare one integer with another through switch case structures without using if statements?
E.g.
switch(integer) {
There is a way, yes. I'm pretty sure I'd use an if/else structure in my own code, but if you're keen to use a switch the following will work:
switch(true) {
case integer >= 1 && integer <= 10:
// 1-10
break;
case integer >= 11 && integer <= 20:
// 11-20
break;
case integer >= 21 && integer <= 30:
// 21-30
break;
}
Of course if you wanted to avoid having to code >= && <=
on every case you could define your own isInRange(num,min,max)
type function to return a boolean and then say:
switch (true) {
case isInRange(integer,1,10):
// 1-10
break;
// etc
}