I think, you can wrap case body in {…}, e.g:
case
{…}
function demo () { const demo = { first: 1 }; switch (demo.first) { case 1: { let result = 1; return result; } case 2: { let result = 2; return result; } case 3: { let result = 3; return result; } default: return demo.first; } }
--
Also, there's a hack that always blows my mind:
(not for production imho, but interesting)
switch (true) { case (demo.first == 1) {…} case (demo.first == 2) {…} case (callSomeFunction(demo.first)) {…} }
Interesting.
Added the pattern to the main article!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
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, you can wrap
case
body in{…}
, e.g:--
Also, there's a hack that always blows my mind:
(not for production imho, but interesting)
Interesting.
Added the pattern to the main article!