Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shorter conditionals in js

Tags:

javascript

I have conditionals like this:

if (foo == 'fgfg' || foo == 'asdf' || foo == 'adsfasdf') {
// do stuff

}

Surely there's a faster way to write this?

Thanks.

like image 687
Mark Avatar asked Jun 21 '10 02:06

Mark


1 Answers

You might consider a switch-case statement

switch(foo) {
  case "fgfg":
  case "asdf":
  case "adsfasdf":
    // ...
}

It's not really any shorter, but could be more readable depending on how many conditions you use.

like image 118
erjiang Avatar answered Sep 23 '22 10:09

erjiang