How can I implement this behavior in JavaScript Switch Statements
expression = 'all' or '1' or '2'
switch (expression)
{
case "all" : print case 1 and case 2
case "1" : print only case 1
case "2" print only case 2
}
How can I do that? If there’s no way to do something like that in JavaScript, I want to know an alternative solution.
>Solution :
You could take two condition instead.
function fn(expression) {
if (expression === '1' || expression === 'all') console.log(1);
if (expression === '2' || expression === 'all') console.log(2);
}
fn('1');
console.log('----------');
fn('2');
console.log('----------');
fn('all');