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
casethat equals the argument of theswitchstatement is executed. So, since the argument istrue, only the firstcasethat have an argument that evaluates totrueis executed. Boom!Also, no. Don't do this. Nobody expects this.