Using || in javascript switches

Is this code possible?

switch (rule)
{
   case 'email' || 'valid_email':
    valid = this.validate_email(field);
    break;
}
+3
source share
2 answers

Close, but this will work:

switch (rule)
{
  case 'email':
  case 'valid_email':
    valid = this.validate_email(field);
    break;
}

The reason it works is because no break;execution continues in the block switch.

+8
source

No, this is not possible; Switch statements do not perform arithmetic calculus.

However, you can use the case chain or the if group:

switch (rule)
{
   case 'email':
   case 'valid_email':
    valid = this.validate_email(field);
    break;
}
+15
source

Source: https://habr.com/ru/post/1712244/


All Articles