Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorter code for this JavaScript "if" statement

Is there a short way to write the following using either JavaScript or jQuery?

if (this.id==="a" || this.id==="b" || this.id==="c" || this.id==="d")
like image 232
Hussein Avatar asked Jan 31 '11 02:01

Hussein


1 Answers

How about this?

if ( this.id in { "a":1, "b":1, "c":1, "d":1 } ) {
  ...
}

... or this?

if("abcd".indexOf(this.id) > -1) {
   ...
}

like image 93
limc Avatar answered Oct 02 '22 22:10

limc