-1

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'));
Liam
  • 461
  • 4
  • 27
  • 2
    "*I couldn't find whether it's possible to write [some] statements […] on one line*" - JavaScript doesn't require linebreaks. You can write **all** statements on a single line, given you separate them with `;`. "*… nor what the best practice*" - the best practice for readability is **not** to write them on the same line. – Bergi Apr 09 '22 at 13:36
  • Thank you very much @Bergi, should I delete my question then, as it doesn't make that much sense anymore? – Liam Apr 09 '22 at 13:57

2 Answers2

1
  • 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.

Liam
  • 461
  • 4
  • 27
  • 3
    I'm pretty sure this is the only way, though it reads terribly and I would 100% make you change it if I was reviewing your code. Readable code is always preferred, unless you're trying to win some contest for least amount of newline characters. – Ryan Wheale Apr 08 '22 at 17:26
  • That would make sense thank you very much for your help. So if you had 20 cases then it's better to have it on 20 lines instead of a compact way like this? – Liam Apr 08 '22 at 17:28
  • 2
    Yeah, I think most would prefer it. You could convince me that two case statements on a single line is OK in this situation - one for the shorthand, one for the longer version. This is ok ONLY if ALL of the shorthands are exactly two characters long. It's worth noting that many people are very good with their editors and will collapse any lines they don't need to see - especially VIM users. It's always best to format your code for readability (see AirBnb ESLINT rules and others), and let developers control the collapsing. – Ryan Wheale Apr 08 '22 at 17:30
  • Yes all the shorthands are only 2 characters long but what you said makes sense, thank you for the advice and help – Liam Apr 08 '22 at 17:32
  • 2
    There's also no need for the blocks you have around your `return` statements. – T.J. Crowder Apr 09 '22 at 14:06
  • Thank you @T.J.Crowder, I've updated my answer now. – Liam Apr 09 '22 at 14:08
-1

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];
Nick Vu
  • 14,512
  • 4
  • 21
  • 31
Nikita S
  • 1
  • 2
  • That might work but the problem is that it's no longer a valid switch statement, the whole point of my question is to use the switch statement. Thanks for your answer though – Liam Apr 08 '22 at 18:19
  • I have not missed the `break` statements, I left them out on purpose since it's a function and therefore when returning the string it doesn't need a break statement. I would also suggest editing your formatting to have clear easy readable text separated from the code. – Liam Apr 08 '22 at 18:21