Front end developer specialising in JavaScript and React. Experienced in all aspects of modern front end development. Passionate about making accessible, secure and performant software.
If it looks cleaner to you and your team, then that's the answer (use it).
Personally, I have a slight preference to normal if / else statements because:
in JavaScript (the language I use at work), the switch statement wouldn't work unless each expression was explicitly true or false. Whereas if / else statements also work with "truthy" or "falsy" values. For example if ('hello' && 'goodbye') passes, because non-empty strings are truthy, but switch(true) { case 'hello' && 'goodbye': } wouldn't pass.
I'm not used to using switch statements like that, so they might catch me out until I get used to them (and others too). If they were the standard convention, then it wouldn't be a problem. As they say, convention over perfection, although this is a very small change in this case.
More realistically, I don't encounter long conditionals often. Short conditionals don't seem like a great use case for switch statements. For example:
if(!condition){returnfoo();}returnbar();
In summary. It's not my cup of tea. But it's not a big change, so I would do it if that's what the team preferred. If you and your team like it, then by all means use it :).
Front end developer specialising in JavaScript and React. Experienced in all aspects of modern front end development. Passionate about making accessible, secure and performant software.
I totally agree with the first point. If you have truthy and falsy value you should prefer if but for long strict conditions, switch can be preferable.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think it depends.
If it looks cleaner to you and your team, then that's the answer (use it).
Personally, I have a slight preference to normal if / else statements because:
true
orfalse
. Whereas if / else statements also work with "truthy" or "falsy" values. For exampleif ('hello' && 'goodbye')
passes, because non-empty strings are truthy, butswitch(true) { case 'hello' && 'goodbye': }
wouldn't pass.In summary. It's not my cup of tea. But it's not a big change, so I would do it if that's what the team preferred. If you and your team like it, then by all means use it :).
I get what you you're saying only
isn't actually checking anything I guess.
The rest are sure valid points! 😊 thnx for sharing
Fair point lol. Off the top of my head I use them sometimes to check for empty strings and stuff :). No problem, thanks for sharing the tip.
I totally agree with the first point. If you have truthy and falsy value you should prefer
if
but for long strict conditions,switch
can be preferable.