91.6k views
2 votes
A default case must be provided for every switch statement. True. False.

1 Answer

6 votes

Final answer:

In a switch statement, a default case is not mandatory, it is optional. It is used when none of the cases match the expression being evaluated.

Step-by-step explanation:

In a switch statement, a default case is not mandatory, it is optional. The default case is used when none of the cases match the expression being evaluated. It acts as a catch-all case and is executed when no other case statements are true.

For example:

switch (dayOfWeek) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
break;
default:
console.log('Not a valid day');
}

In this case, if the value of dayOfWeek is neither 1 nor 2, the default case will be executed and 'Not a valid day' will be printed.

User Nicktmro
by
7.5k points