Ali
Ali

Reputation: 267177

Using || in javascript switches

Is this code possible?

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

Upvotes: 3

Views: 240

Answers (2)

Andrew Moore
Andrew Moore

Reputation: 95424

Close, but this will work:

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

The reason why it works is that without a break;, execution continues within the switch block.

Upvotes: 8

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

No, it is not possible, Switch statements doesn't do arithmetic calculus.

However, you can use case chaining or a bunch of if's:

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

Upvotes: 15

Related Questions