switch statement with Range of values in Javascript

By Santhosh N

This explains how to have range of values in case option inside the Javascript switch statement.

You can validate cases based on the range of values in the case statement inside the switch in javascript and here is sample code how to do this. Remember you need to have boolean true value as variable in the switch statement but not the variable you wish to validate as in normal cases

var selIndex = 0;
var val = 100;
        case ((val >= 1) && (val <= 10)):
            selIndex = 0;
        case ((val >= 11) && (val <= 100)):
            selIndex = 1;
        case ((val >= 101) && (val <= 1000)):
            selIndex = 2;
        case ((val >= 1001) && (val <= 10000)):
            selIndex = 3;


Related FAQs

This is how trimming of a string can be done in javascript is simpler manner.
This is a way to check if the given string is in numeric format or not
Get the random number within the specified range
Simple statement to add a site to Favourites collection of Browser from Javascript
You can reset all the page controls in webpage using a single statement in Javascript
JQuery is so simple to use when compared to traditional javascript and here you can see how easily you can add and remove css classes for the element using jQuery
switch statement with Range of values in Javascript  (3012 Views)