
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: BEGINNER
Assumes good beginner level knowledge and/or familiarity of Scripting in ServiceNow.
JavaScript Switch Statements - A useful alternative to infinite if then/else if/else statements!
I see this a lot in code scripts: A run-on if (...) {} else if (...) {} else if (...) {} else if (...) {} else if (...) {} else {} statement. While they have their uses (especially with mixed variable checks); they can be confusing to a maintenance coder (even if well commented). Might I suggest that if you find yourself faced with the need for a large if/then statement that examines the same variable that you use a 'switch' statement instead. With this sort of situation; 'switch' statements can organize the code in a more maintainable manner, and allow for easier condition additions at a later time.
NOTE: I ran all of these examples Scripts - Background.
Here is an example. First the if then/else if, and then the same thing re-written as a 'switch' statement:
// If/then/else if/else
var myCheck = 'yellow';
var temperature = '0 F';
if (myCheck == 'green') {
gs.info("(if) All is well with the 'verse!");
temperature = '72 F';
}
else if (myCheck == 'yellow' || myCheck == 'yellow-orange') {
gs.info('(if) The Dallas Cowboys really reside in Arlington.');
temperature = '85 F';
}
else if (myCheck == 'orange' || myCheck == 'orange-red') {
if (myCheck == 'orange') {
gs.info('(if) Temperatures in Dallas are hotter than in Houston, and the humidity is worse!!!');
temperature = '95 F';
}
if (myCheck == 'orange' || myCheck == 'orange-red') {
gs.info('(if) Wait, it is hotter than I thought!!');
temperature = '98 F';
}
}
else if (myCheck == 'red') {
gs.info('(if) Red alert! Red alert! Danger, danger Will Robinson!');
temperature = '100 F';
}
else {
gs.info('(if) I have no idea what the myCheck value means!');
temperature = '>100 F!';
}
gs.info('(if) The temperature is: ' + temperature);
Will give the following result (it is our baseline):
So here is the basic switch alternative:
// switch analog - basic
var myCheck = 'yellow';
var temperature = '0 F';
switch (myCheck) {
case 'green':
gs.info("(switch-basic) All is well with the 'verse!");
temperature = '72 F';
break;
case 'yellow':
// No break here. Falls through.
case 'yellow-orange':
gs.info('(switch-basic) The Dallas Cowboys really reside in Arlington.');
temperature = '85 F';
break;
case 'orange':
gs.info('(switch-basic) Temperatures in Dallas are hotter than in Houston, and the humidity is worse!!!');
temperature = '95 F';
break;
case 'orange-red':
gs.info('(switch-basic) Wait, it is hotter than I thought!!');
temperature = '98 F';
break;
case 'red':
gs.info('(switch-basic) Red alert! Red alert! Danger, danger Will Robinson!');
temperature = '100 F';
break;
default:
gs.info('(switch-basic) I have no idea what the myCheck value means!');
temperature = '>100 F';
break;
}
gs.info('(switch-basic) The temperature is: ' + temperature);
Will give the following result:
Aaaand, it is the same; except the label of course.
Notice that with the 'switch' it is a simple matter to add new conditions ('else if') by inserting a new 'case' statement. If you want to add an 'OR' into the mix simply add a new 'case' condition before the code you want executed (yellow OR yellow-orange). The 'default' statement acts as the catch-all (the same as the 'else' statement in the 'if/then' structure). BTW, the 'default' statement is optional (the same as the 'else' is optional in the 'if then/else if/else' statement). Don't forget the 'break' statement as this serves to keep the program from executing the lines immediately following! Try out the 'orange' value to see what I mean.
Another approach on the switch statement gives the developer much more flexibility on the Case statements. For example:
// switch statement - flexible version
var myCheck = 'yellow';
var temperature = '0 F';
switch (true) {
case (myCheck == 'green'):
gs.info("(switch-flexible) All is well with the 'verse!");
temperature = '72 F';
break;
case (myCheck == 'yellow' || myCheck=='yellow-orange'):
gs.info('(switch-flexible) The Dallas Cowboys really reside in Arlington.');
temperature = '85 F';
break;
case (myCheck == 'orange'):
gs.info('(switch-flexible) Temperatures in Dallas are hotter than in Houston, and the humidity is worse!!!');
temperature = '95 F';
break;
case (myCheck == 'orange-red'):
gs.info('(switch-flexible) Wait, it is hotter than I thought!!');
temperature = '98 F';
break;
case (myCheck == 'red'):
gs.info('(switch-flexible) Red alert! Red alert! Danger, danger Will Robinson!');
temperature = '100 F';
break;
default:
gs.info('(switch-flexible) I have no idea what the myCheck value means!');
temperature = '>100 F';
break;
}
gs.info('(switch-flexible) The temperature is: ' + temperature);
Will give the following, same, result:
There you go, another tool for your toolbox!
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 07-19-2015 08:16 AM
I updated the code and brought the article into alignment with my new formatting standard.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.