This works for regex but what if you also want to check variable types, whether a key exist in an object, or the number of elements in a array, then just a big slew of ifs are your only choice.
Not really - this technique works with anything - it just compares true with the result of whatever expression you put in the case. Doesn't need to be regex. For example - testing a key is in an object:
switch(true){case'red'inobj:console.log("has a red property")breakcase'blue'inobj:console.log("has a blue property")breakcase'green'inobj:console.log("has a green property")breakdefault:console.log("none of them")}
Granted - this isn't really that different to writing a bunch of ifs, but it works and could be considered more readable in some cases
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.
Not really - this technique works with anything - it just compares
true
with the result of whatever expression you put in the case. Doesn't need to be regex. For example - testing a key is in an object:Granted - this isn't really that different to writing a bunch of
if
s, but it works and could be considered more readable in some cases