Include integer ranges in JavaScript

I want to do something like this

switch (this.dealer) { case 1-4: // Do something. break; case 5-8: // Do something. break; case 9-11: // Do something. break; default: break; } 

What is the correct syntax for this? Is this possible in JavaScript?

So this.dealer is an integer, and if it is between these values, do something.

+83
javascript switch-statement
Apr 11 '11 at 10:15
source share
9 answers

Here is another way that I understood this:

 var x = this.dealer; switch (true) { case (x < 5): alert("less than five"); break; case (x > 4 && x < 9): alert("between 5 and 8"); break; case (x > 8 && x < 12): alert("between 9 and 11"); break; default: alert("none"); break; } 
+168
Apr 11 2018-11-11T00:
source share

Increment MarvinLabs answer to make it cleaner:

 var x = this.dealer; switch (true) { case (x < 5): alert("less than five"); break; case (x < 9): alert("between 5 and 8"); break; case (x < 12): alert("between 9 and 11"); break; default: alert("none"); break; } 

There is no need to check the lower limit of the range, because break statements will force execution to skip the remaining cases, so by the time execution is checked, for example. (x <9), we know that the value must be 5 or more.

Of course, the result is correct if the cases remain in the original order, and we accept integer values ​​(as indicated in the question) - technically, the ranges are between 5 and 8.999999999999 or so, since all numbers in js actually double-digit floating-point numbers.

If you want to be able to move cases around or find them more readable so that the entire range is displayed in each case, just add a less or equal check for a lower range for each case:

 var x = this.dealer; switch (true) { case (x < 5): alert("less than five"); break; case (x >= 5 && x < 9): alert("between 5 and 8"); break; case (x >= 9 && x < 12): alert("between 9 and 11"); break; default: alert("none"); break; } 

Keep in mind that this adds an additional point of human error - someone may try to update the range, but forget to change it in both places, leaving an overlap or a space that does not cover. for example, here case 8 will no longer match anything when I just edit the case that was used to match 8.

  case (x >= 5 && x < 8): alert("between 5 and 7"); break; case (x >= 9 && x < 12): alert("between 9 and 11"); break; 
+31
09 Oct '13 at 23:44
source share
  switch(this.dealer) { case 1: case 2: case 3: case 4: // Do something. break; case 5: case 6: case 7: case 8: // Do something. break; default: break; } 

If you don't like the sequence of things, just go to the if/else if/else .

+16
Apr 11 '11 at 10:17
source share

This one does not require a switch statement. It is much clearer , more concise and faster to just use else statements ...

 var d = this.dealer; if (1 <= d && d <= 11) { // making sure in range 1..11 if (d <= 4) { alert("1 to 4"); } else if (d <= 8) { alert("5 to 8"); } else { alert("9 to 11"); } } else { alert("not in range"); } 

Speed ​​test

I was interested to learn how to use the switch instead of simple if ... else ..., so I put together jsFiddle to learn it ... http://jsfiddle.net/17x9w1eL/

  • Chrome: switch was about 70% slower than if else

  • Firefox: switch was about 5% slower than if else

  • IE: the switch was about 5% slower than if else

  • Safari: switch was about 95% slower than if else

Notes:

Assigning a local variable is optional, especially if your code is automatically optimized later.

For numeric ranges, I like to use this construct ...

 if (1 <= d && d <= 11) {...} 

... because for me it reads closer to how you express the range in mathematics (1 <= d <= 11), and when I read the code, I can read it as "if d is between 1 and 11".

Clearer

Several people noted that they did not consider this more clear. This, of course, is no less clear, since the structure is close to the identical switch option. The main reason that it is more understandable is that every part of it is readable and makes simple intuitive meaning, while "switch (true)" is a rather meaningless line of code. Many coders reading what is in your script are going to go "Does WTF mean?" and then you have to look for him. This is a dirty hack, it is not intuitive, and it is incomprehensible. If you like the code and no one else will be dealing with your code base, then go for it, otherwise it's better to just use the constructs for what they are intended to be.

+11
Jan 10 '15 at 1:21
source share

No, It is Immpossible. The closest you can get is:

  switch(this.dealer) { case 1: case 2: case 3: case 4: // DO SMTHIN break; case 5: case 6: case 7: case 8: // DO SMTHIN break; 

But this is very awkward.

For such cases, it is usually best to use an if / else if structure.

+1
Apr 11
source share

If you need ranges of checks, you will probably be better off with if and else if , for example:

 if (range > 0 && range < 5) { // .. } else if (range > 5 && range < 9) { // .. } else { // Fall through } 

The switch can get big on large ranges.

0
Apr 11 2018-11-11T00:
source share

If you are trying to do something quickly, efficiently and readably, use a standard, if ... then ... else structure like this:

 var d = this.dealer; if (d < 12) { if (d < 5) { alert("less than five"); }else if (d < 9) { alert("between 5 and 8"); }else{ alert("between 9 and 11"); } }else{ alert("none"); } 

If you want to confuse it and make it horrible (but small), try the following:

 var d=this.dealer;d<12?(d<5?alert("less than five"):d<9?alert("between 5 and 8"):alert("between 9 and 11")):alert("none"); 

By the way, the above code is JavaScript, if ... then ... else is an abridged statement. This is a great example of how NOT to write code if the goal is to obfuscate or encode the code. Keep in mind that servicing code can be a problem if it is written this way. Very few people can read it easily, if at all. The code size, however, is 50% smaller than the standard, if ... then ... still without loss of performance. This means that in large code versions this minimization can significantly speed up code delivery over networks with limited bandwidth or high latency.

This, however, should not be considered a good answer. This is just an example of what MAY be done, and not what MUST be done.

0
Jun 06 '17 at 21:02
source share

A more readable version of the triple might look like this:

 var x = this.dealer; alert(t < 1 || t > 11 ? 'none' : t < 5 ? 'less than five' : t <= 8 ? 'between 5 and 8' : 'Between 9 and 11'); 
0
Jun 13 '17 at 4:08 on
source share
 function sequentialSizes(val) { var answer = ""; switch (val){ case 1: case 2: case 3: case 4: answer="Less than five"; break; case 5: case 6: case 7: case 8: answer="less than 9"; break; case 8: case 10: case 11: answer="More than 10"; break; } return answer; } // Change this value to test you code to confirm ;) sequentialSizes(1); 
0
Jul 04 '17 at 6:57
source share



All Articles