Switch Case in JavaScript Example Programs

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?

Similar Articles

Comments

Ads

Most Popular