Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Combination inside switch case

I used to have a simple switch case, like this:

const expr = 'Papayas';
switch (expr) {
  case 'Oranges':
    console.log('Oranges');
    break;
  case 'Mangoes':
    console.log('Mangoes');
    break;
  case 'Papayas':
    console.log('Papayas');
    break;
}

but now some criteria changed and I need to add one rule for all three cases and leave the old functionality as it was. So in case, I'll have Papayas, my resalt would be All and Papayas. I tried to combine it like this:

const expr = 'Papayas';
switch (expr) {
  case 'Oranges':
  case 'Mangoes':
  case 'Papayas':
    console.log('All');
  case 'Oranges':
    console.log('Oranges');
    break;
  case 'Mangoes':
    console.log('Mangoes');
    break;
  case 'Papayas':
    console.log('Papayas');
    break;
}

but that doesn't work correctly. I always get All and Oranges in the result. Is there a way to do it with one switch case?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can make something like this:

const output = ['All'];

switch (expr) {
  case 'Oranges':
    output.push('Oranges');
    break;
  case 'Mangoes':
    output.push('Mangoes');
    break;
  case 'Papayas':
    output.push('Papayas');
    break;
}

console.log(...output);
about 4 years ago · Juan Pablo Isaza Report

0

What happens if I forgot a break?

If you forget a break then the script will run from the case where the criterion is met and will run the cases after that regardless if a criterion was met. - the MDN JS docs on switch

What happens in your case is that it matches the case 'Papayas': console.log('All') and then runs the next case 'Oranges': console.log('Oranges') break; despite it not being met; afterwards comes the break of that case - without this you'd also be outputting Mangoes and Papayas.

The first, trivial fix is to just duplicate the console.log('All') in all switch cases:

switch (expr) {
  case 'Oranges': console.log('All'); console.log('Oranges'); break;
  case 'Mangoes': console.log('All'); console.log('Mangoes'); break;
  case 'Papayas': console.log('All'); console.log('Papayas');
}

the last break may be omitted. An alternative that requires the use of a second switch and duplicating the cases is to just execute one switch after another:

let val = expr;

switch (val) {
  case 'Oranges':
  case 'Mangoes':
  case 'Papayas':
    console.log('All')
}

switch (val) {
  case 'Oranges': console.log('Oranges'); break;
  case 'Mangoes': console.log('Mangoes'); break;
  case 'Papayas': console.log('Papayas'); break;
}

but I'd question whether a switch is the appropriate language construct to use here. An if comes to mind:

let val = expr;
if (val == 'Oranges' || val == 'Mangoes' || val == 'Papayas') {
  console.log('All');
  switch (val) {
    case 'Oranges': console.log('Oranges'); break;
    case 'Mangoes': console.log('Mangoes'); break;
    case 'Papayas': console.log('Papayas'); break;
  }
}

Ideally, you can look up the message in a dictionary / "object" in JS terminology:

const messages = {
  Oranges: 'Oranges',
  Mangoes: 'Mangoes',
  Papayas: 'Papayas'
}
let message = messages[expr];
if (message) {
  console.log('All');
  console.log(message);
}

Alternatively using arrow funcs if your code doesn't consist just of a mapping data -> data but rather data -> action:

const funcs = {
  Oranges: () => console.log('Oranges'),
  Mangoes: () => console.log('Mangoes'),
  Papayas: () => console.log('Papayas')
}
let func = funcs[expr];
if (func) {
  console.log('All');
  func();
}
about 4 years ago · Juan Pablo Isaza Report

0

You could take advantage of the way select statements will continue after matching the first item if a break; isn't declared. Example below uses conditional breaks to output the following.

  • Input: 'All' = All - Oranges Mangoes and Papayas
  • Input: 'Oranges' = Oranges
  • Input: 'MangPap' = All - Mangoes and Papayas
  • Input: 'Mangoes' = Mangoes
  • Input: 'Papayas' = Papayas
  • Input: unrecognized (default) = Input not recognized.

const expr = 'MangPap';
var output = '';
switch (expr) {
  case 'All':
    output = 'All - ';
  case 'Oranges':
    if ( output.length > 0 ) { output += 'Oranges'; } else { output += 'Oranges'; }
    if (expr === 'Oranges') { break; }
  case 'MangPap':
    if ( output.length > 0 ) {  } else { output += 'All - '; }
  case 'Mangoes':
    if ( output.length > 0 ) { output += ' Mangoes'; } else { output += 'Mangoes'; }
    if (expr === 'Mangoes') { break; }
  case 'Papayas':
    if ( output.length > 0 ) { output += ' and Papayas'; } else { output += 'Papayas'; }
    if (expr === 'Papayas') { break; }
  default:
    if ( output.length > 0 ) {  } else { output += output = 'Input not recognized.'; }
}
console.log( output );

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!