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.log('Consider using an if-else patter...
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...
Sometimes, we may want multiple case values to trigger the same block of code. For this, we can use multiple cases with a single block. Let's look at the example below to understand this clearly. // Program to categorize age let age = 19; switch (age) { // when age is 13, 14,...
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...
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....
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 ...
case 'potato': default: console.log('This is not a fruit.'); } When you run the above-written code, you will get the output as shown below: >“This is a fruit.” Like an example shown above, we can use the switch statement for multiple cases making our work easier. ...
Here is a basic example of a block of code that contains anifstatement, multipleelse ifstatements, and anelsestatement in case none of the conditions evaluated totrue. if(condition a){// code that will execute if condition a is true}elseif(condition b){// code that will execute if cond...
For multiple conditions, you should consider using the switch statement if the conditions are based on string or numeric value equivalency: switch (expression) {casevalueA: // statements to execute ifexpressionevaluates tovalueAbreak; // skip over default casevalueB:// statements to execute ifexp...