Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript Switch case statement : fall-through all cases when expression is 'all'

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading