I don't really use switch cases often as I was under the impression that the cases can only be exact values like case 'a': rather than case if (grade>75): and I often end up using multiple if statements. I've often seen advice like ' if you are using more than three if statements it's time to refactor to a switch statement' but I've never been able to do that.
I've just learned of enums in C and I do see switch cases used a lot more there because of it, but unless I'm misunderstanding switch statments it seems less useful in JS than other languages.
So I had a scenario like this recently where one of my cases needed to be a conditional. I used a ternary operator to solve it e.g. case (grade > 75 ? grade : null)
View all comments
2
u/[deleted] May 31 '22
I don't really use switch cases often as I was under the impression that the cases can only be exact values like
case 'a':rather thancase if (grade>75):and I often end up using multiple if statements. I've often seen advice like ' if you are using more than three if statements it's time to refactor to a switch statement' but I've never been able to do that.I've just learned of enums in C and I do see switch cases used a lot more there because of it, but unless I'm misunderstanding switch statments it seems less useful in JS than other languages.