I have conditionals like this:

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


Surely there's a faster way to write this?


+4  A: 
if (/^(fgfg|asdf|adsfasdf)$/.test(foo)) {


if (["fgfg", "asdf", "adsfasdf"].indexOf(foo) != -1) {

Cross-browser support for Array.indexOf is still limited. Also, these are faster to write, probably not faster to run.

Matthew Flaschen
Your first line returns true for a string that only contains one of those words. "asdfz" would trigger the conditional, for example.
@mazin, yeah, I actually added the missing anchor before seeing your comment.
Matthew Flaschen
+1 for `indexOf`, -1 for using regexp. :P I kid, I kid, +1 from me.
Just to note, you can write your own `indexOf` function. MDC has [an example](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf).

Depending on the situation you could do..

//At some point in your code
var vals = new Array('fgfg', 'asdf', 'adsfasdf');
if(vals.indexOf(foo) >= 0)
Matthew Flaschen's is better.
@Duracell - You should look at it in terms of performance as well, when the options list grows, regex performance goes down the drain, `.indexOf()` remains a very low cost, if you instantiate the array once it's better of course.
Nick Craver
@Nick - Of course, "very low cost" is a relative term.
+5  A: 

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.

+4  A: 

I would keep the conditionals the way they are. Any clever way of shortening them would make the code less idiomatic and less readable.

Now, if you do care about readability, you could define a function to do the comparison:

if( foo_satisfies_condition(foo) ) {
  // ...


if( is_month_name(foo) {
  // ...

If you give the function a name that faithfully describes what it does, it will be easier to understand the intent of the code.

How you implement that function would depend on how many comparisons you need. If you have a really large number of strings you're comparing against, you could use a hash. The implementation details are irrelevant when reading the calling code, though.

Ori Pessach
+3  A: 

No need for using indexOf or a regex if you just use a hash table:

var things = { 'fgfg' : 1, 'asdf' : 1, 'asdfasdf' : 1 };
if ( things[foo] ) { 
I think this would perform quite well.
Honestly, I think this is less readable than just writing conditionals, and I wouldn't think it would be faster to write.
If you think that's less readable, I sure wouldn't want to be tasked with working on your code. :) Nothing bugs me more than seeing a hugely complex conditional glued together with a dozen boolean operators when a lookup table would make things so much easier.
+1  A: 

Here's a easy way:

String.prototype.testList = function(lst) {
 lst = lst.split('|');
 for(var i=0; i<lst.length; i++){
  if (this == lst[i]) return true;
 return false;

To use this function, you can just do this:

if (foo.testList('fgfg|asdf|adsfasdf')) {

You can also rename testList to whatever you want, and change the delimiter from | to anything you want.

Dumb Guy
I would invert the sense of the test, eg: String.prototype.hasToken = function(token) { return this.split(/\s+/).indexOf(token) != -1; }.
Sean Hogan

Ternary operator looks good if you like and has else