Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand for multiple OR expressions in if statement

Is there a shorthand for the following -

if(tld == "com" || tld == "net" || tld == "co" || tld == "org" || tld == "info" || tld == "biz")
{
    //do something;
}
like image 370
Shalom Sam Avatar asked Jun 20 '12 20:06

Shalom Sam


1 Answers

you could use an array

if(["","com","net","co","org","info","biz"].indexOf(tld) > -1) {
     // do something
}

or if you are using jquery :

$.inArray(tld, ["com","net","co","org","info","biz"])

REF - Performance of OR operation ( || ) vs inArray()

like image 179
hackartist Avatar answered Nov 10 '22 01:11

hackartist