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

229
Views
Compact one-liner for each case statement in a switch statement - JavaScript

I couldn't find any documentation or questions about whether it's possible to write the case statements in a switch statement on one line nor what the best practice and way to write them on one line in JavaScript is.

The most related questions were: CodeGrepper Question and Switch statement for multiple cases in JavaScript

I currently have a function with a switch statement that returns a product's name:

function returnProduct(product) {
    switch (product) {
        case 'aa': case 'AApple':
            return 'AApple';
        case 'bb': case 'BananaBall':
            return 'BananaBall';
        case 'ee': case 'ElephantElf':
            return 'ElephantElf';
        default:
            return 'Sorry we don\'t have that yet';
    }
}

console.log(returnProduct('aa'));
console.log(returnProduct('error'));
console.log(returnProduct('ee'));
console.log(returnProduct('BananaBall'));

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

  • Solution 1

    Thanks to @Ryan Wheale, @Bergi and @T.J. Crowder who helped me realize that since Javascript doesn't require line breaks it could all be written on one single line separated by; and I don't need to put any {} around the return statements. While the following code snippet does answer my question, it's also important to mention that a one liner like this is not at all readable and should be avoided.

function returnProduct(product) { switch (product) { case 'aa': case 'AApple': return 'AApple'; case 'bb': case 'BananaBall': return 'BananaBall'; case 'ee': case 'ElephantElf': return 'ElephantElf'; default: return 'Sorry we don\'t have that yet'; } } console.log(returnProduct('aa')); console.log(returnProduct('error')); console.log(returnProduct('ee')); console.log(returnProduct('BananaBall'));


  • Solution 2: This solution is a bit more readable and still achieves my original goal of creating a compact switch statement.

function returnProduct(product) {
        switch (product) {
            case 'aa': case 'AApple': return 'AApple';
            case 'bb': case 'BananaBall': return 'BananaBall';
            case 'ee': case 'ElephantElf': return 'ElephantElf';
            default: return 'Sorry we don\'t have that yet';
        }
    }

    console.log(returnProduct('aa'));
    console.log(returnProduct('error'));
    console.log(returnProduct('ee'));
    console.log(returnProduct('BananaBall'));

This was my first proposed solution which solves my problem but I hadn't tested it enough and had some errors which the community helped me fix.

I hope that others asking the same question will be able to find this as a useful answer to the same question I had.

over 4 years ago · Santiago Trujillo Report

0

Seems you have missed the break statement. Or Try using dictionary

var x ='aa':'AApple','AApple':'AApple','bb':'BananaBall', 
       'BananaBall':'BananaBall'};
var key='aa';
var z;
if(x[key] !== undefined)
    return x[key];
over 4 years ago · Santiago Trujillo 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!