- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-07-2021 09:04 AM
Hello all,
Use the switch statement to select one of many code blocks to be executed.
This is how it works:
- The switch expression is evaluated once.
- The value of the expression is compared with the values of each case.
- If there is a match, the associated block of code is executed.
- If there is no match, the default code block is executed.
Note: The default keyword specifies the code to run if there is no case match. The default case does not have to be the last case in a switch block
Example
The getDay(): the method returns the weekday as a number between 0 and 6.
(e.g. Sunday=0, Monday=1, Tuesday=2 ..)
This example uses the weekday number to calculate the weekday name:
-----------------------------------------------------------------------
var day = '';
var dayNr = new Date().getDay();
switch (dayNr) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
default:
day = "Unknown!";
}
gs.info("Today is " + day);
-----------------------------------------------------------------------
The result will be:
Today is Tuesday
-----------------------------------------------------------------------
Thanks for visiting my article. If the article helped you in any way, please hit the like button/mark it helpful. So it will help others to get the correct solution.
- 13,776 Views