This is messed up and you shouldn't do it, but we can sort-of have pattern matching in JavaScript:
switch(true){case200<=status&&status<300:{console.log('Success!');break;}casestatus===418:{console.log("I'm a 🫖");break;}default:throwError('Something went wrong!');}
The trick here is that the firstcase that equals the argument of the switch statement is executed. So, since the argument is true, only the first case that have an argument that evaluates to true is executed. Boom!
Also, no. Don't do this. Nobody expects this.
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability.
This is messed up and you shouldn't do it, but we can sort-of have pattern matching in JavaScript:
The trick here is that the first
case
that equals the argument of theswitch
statement is executed. So, since the argument istrue
, only the firstcase
that have an argument that evaluates totrue
is executed. Boom!Also, no. Don't do this. Nobody expects this.