You have to use thelogical OR, AND operatorsin the switch case to use multiple values in JavaScript. JavaScript switch case multiple values Simple example code test multiple conditions in a JavaScript switch statement. Here is theOR condition, anyone true will execute the case block. <!DOCTYPE h...
switch(yourUseCase) {case'large_number_of_conditions':case'single_variable_evaluation':case'multiple_discrete_values':console.log('Consider using a switch statement.');break;case'complex_conditions':case'range_based_conditions':case'non_constant_cases':console.l...
switch(yourUseCase) {case'large_number_of_conditions':case'single_variable_evaluation':case'multiple_discrete_values':console.log('Consider using a switch statement.');break;case'complex_conditions':case'range_based_conditions':case'non_constant_cases':console.log('Consider using an if-else patter...
letday =3;letactivity;switch(day) {case1:console.log("Sunday");break;case2:console.log("Monday");break;case3:console.log("Tuesday");break;case4:console.log("Wednesday");break;case5:console.log("Thursday");break;case6:console.log("Friday");break;case7:console.log("Saturday");break;d...
The Switch statement can be used in place of the If statement when you have many possible conditions.In the previous lesson about JavaScript If statements, we learned that we can use an If Else If statement to test for multiple conditions, then output a different result for each condition....
The switch is a statement used to perform actions based on conditions. Its functionality matches that of the if-else statement. You might be guessing why there are two keywords with the same functionality. There are some such cases where we prefer usi
switch(newDate().getDay()) { case4: case5: text ="Soon it is Weekend"; break; case0: case6: text ="It is Weekend"; break; default: text ="Looking forward to the Weekend"; } Try it Yourself » Switching Details If multiple cases matches a case value, thefirstcase is selected...
This is not a design error per se, but a desirable thing in certain scenarios when we want multiple cases to give the exact same outcome. We'll see the exact details of this in the chapter JavaScript Conditions — The switch Statement. For now, you should stick to using break at the ...
eslint rules: no-case-declarations.```javascript // bad switch (foo) { case 1: let x = 1; break; case 2: const y = 2; break; case 3: function f() { // ... } break; default: class C {} } // good switch (foo) { case 1: { let x = 1; break; } case 2: { ...
For multiple conditions, you should consider using the switch statement if the conditions are based on string or numeric value equivalency: switch (expression) { case valueA: // statements to execute if expression evaluates to valueA break; // skip over default case valueB: // statements to ...