The switch statement evaluates the expression, compares the result with the case value, How switch case is used in JavaScript? and executes the statement associated with that case value.
The following illustrates the switch statement syntax:
switch (expression)
case value1:
statement1;
disturbance;
case value 2:
statement2;
disturbance;
case value3:
statement3;
disturbance;
default:
statement;}
How it works.
Firstly, evaluate the expression in parentheses after the switch keyword.
Secondly, compare the results of the expression with values ​​1, values ​​2, … in a random branch from top to bottom. The switch statement employs a strict contrast (===).
Thirdly, run the statement on the case branch, where the result of the expression is equal to the value following the case keyword. If you skip the break statement, code execution will skip the original case branch in the next. If the result of the expression does not always equal the value, the switch statement executes the statement on the default branch.
That the switch statement stops comparing the result of the expression with other values ​​of the
main case as long as it finds a match.
A switch statement is similar to the if…else…if kind of statement but with a more readable syntax.
A switch statement is used to substitute a complex if…else..if statement to ensure that the code is more readable.
How it works.
The first thing is to declare a day variable which consists of the day number and the day name variable (dayName).
Thereafter, retrieve the day of the week by day number with the switch statement. If day is 2, the day of the week is Monday. If the day is 3, the day of the week is Tuesday, and so on.
The third and final thing is to bring the day of the week to the console
Use JavaScript redirect statement to get number of days by month
The demonstration below uses the switch statement to obtain the number of days in a particular month:
let year = 2022;
let month = 2;
let the days count;
switch (month) {
Case 1:
Case 3:
Case 12:
dayCount = 31;
break;
Case 4:
Case 6::
dayCount = 30;
break;
Case 2:
// leap year
If (year% 4 == 0 &&! (year% 100 == 0)) || year %400 == 0) {number of days = 29;} Something else {number of days = 28;}
break;
default:
dayCount = -1; // invalid month
console.log (number of days); // 29
the example above provides four scenarios:
Suppose the month is 1, 3, or 12, the number of days in that month is 31.
Suppose the month is 4, 6, or 11, the number of days in that month is 30.
Perhaps, if the month is 2 and the year is not long, the number of days is 28. If the year is high, the number of days is 29.
In the event that the month is not in the valid range (1-12), the default branch will accomplish and set the dayCount variable to -1, indicating an invalid month.
Conclusion
The switch statement assesses the expression, equivalences the result with the case value, and completes the statements related to that case.
HOW DO I FIND MY NEXT READ?
- CISCO PRIVILEGE LEVELS LIST
- HOW TO GET WI-FI PASSWORD OF NEIGHBOR’S USING LAPTOP
- HOW DO I FIND MY WORDPRESS ADMIN IP ADDRESS
- FIX CAN’T LOG INTO HUAWEI ROUTER
- HOW TO SETUP HUAWEI 4G ROUTER
- HOW TO CONFIGURE HUAWEI ROUTER AS WIRELESS ACCESS POINT
- HOW TO ENTER BIOS WINDOWS 10 WITHOUT UEFI
- HOW TO ENTER BIOS WINDOWS 10 LENOVO
- HOW TO CREATE FAT32 BOOTABLE USB WINDOWS 10
- CISCO PRIVILEGE LEVELS LIST
- MS WORD COMMANDS LIST (SHORTCUT KEYS OF MS WORD 2010)
- SHORTCUT KEY FOR SAVE AS IN MS WORD 2010
- MAKE MONEY BLOGGING
- ROTATE SCREEN WINDOWS 7 KEYBOARD SHORTCUT
- 15 PLACES TO HOST A WORDPRESS WEBSITE FOR FREE
- HOW TO ACCESS WORDPRESS ADMINISTRATION WITHOUT DOMAIN
- MAKE MONEY WITH WORDPRESS IN 48 HOURS
- CHARGE FOR A BLOG POST IN THE UK
- HOW DO I FIND MY WORDPRESS ADMIN IP ADDRESS
- HOW I MAKE MY WORDPRESS THEME NULLED
- 10 REASONS IT IS NOT SAFE TO USE NULLED WORDPRESS THEME
- 10 WAYS WORDPRESS THEMES CAN BE HACKED
- CHARGE FOR A BLOG POST IN INDIA
- LENOVO IDEAPAD 100 BIOS KEY SETUP PROCESS
- COST TO START A BLOG IN INDIA
- HUAWEI WI-FI REPEATER SETUP
- HOW TO USE HUAWEI ROUTER AS WI-FI EXTENDER
- THE USE OF REPEATERS IN COMMUNICATION
- WHAT IS THE DIFFERENCE BETWEEN A BASE STATION AND REPEATER?
- HOW TO BOOST 4G SIGNAL INDOORS
- HOW TO INCREASE MOBILE INTERNET SPEED
- DOES WI-FI USE MICROWAVES OR RADIO WAVES?
- WHAT TYPE OF ELECTROMAGNETIC WAVES ARE USED IN BLUETOOTH DEVICES?