67

In SQL Server, I could say:

WHERE X IN(1,2)

How would you rewrite the following in JavaScript:

if (X==1 || X==2) {}
2
  • I wonder if I could write an IN function. Commented Mar 9, 2011 at 21:54
  • 4
    Your question doesn't make sense. You have written valid JavaScript. Commented Mar 9, 2011 at 21:55

9 Answers 9

90

Use indexOf to see if x is in an array.

if([1,2].indexOf(x) !== -1)
Sign up to request clarification or add additional context in comments.

1 Comment

Browser compatibility of Array.indexOf looks good, starts with MSIE 9 for example developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
24

Using Array .includes method.

if ([1, 2].includes(x)) {
  // array has x
}

4 Comments

This is a better answer for modern browsers, but I'm afraid IE doesn't support it. Ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
It's good practice on Stack Overflow to add an explanation as to why your solution should work. For more information read How To Answer.
Yes, IE won't work. Use it with polyfill or in conjunction with a transpiler (e.g. Babel / Typescript)
It makes me cooler when my answer is short.
9

Try using an array, and then its .indexOf().

 var myNumbers = [1,2];
 var foo = 4;
 var bar = 1;

 var exists = (myNumbers.indexOf(bar) > -1); //true
 var notExists = (myNumbers.indexOf(foo) > -1); //false

2 Comments

Note: Not all browsers come with Array.indexOf
IE7 and below don't support it apparently.. here's more info and a drop in fallback soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer
7

There's no silver bullet. There will be a few gotchas.

If you do indexOf as some answers suggest, you need to remember that Array.indexOf is not supported in all browsers, so you need to provide your own fallback. Also this will have a performance of O(n) since it needs to traverse the whole array, which might not be ideal if you're dealing with a huge array.

If you use the in operator as other answers suggest, you need to remember that in Javascript object's properties are always strings, so don't expect === checks to work if you're checking for numbers.

In this particular example you suggested, I would just go for the good old if (X==1 || X==2).

1 Comment

simple and super clean answer that assure to run on all browsers.
3
if (x in {1:true, 2:true}) { }

Or, if you want to abstract it you could do it like this http://snook.ca/archives/javascript/testing_for_a_v

function oc(a) { var o = {}; for(var i=0;i

Still... not the most scalable thing to be doing

2 Comments

This fails If x contains an identifier of an “inherited” property: var x = "constructor"; x in {1:true, 2:true} yields true.
Thats interesting, to be honest it isn't really the nicest construct anyway.
2

Requires Javascript 1.6

if ((new Array(1, 2)).indexOf(X) != -1) {
}

Comments

2

I know we have in_array() function in PHP, but I never heard of a similar function in JS. I think you gotta do it the old way:

function contains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}

Comments

2

Function to convert the array into an object literal

function oc(a)
{
 var o = {};
 for(var i=0;i<a.length;i++)
 {
  o[a[i]]='';
 }
  return o;
}

You can call the function like

if( name in oc(['Tom', 'Harry','Sue']) ) { ... }

Comments

1

Just fun:

if (X*X+1*2 == (1+2)*X) {}

or self-explaining:

if ((X-1)*(X-2) == 0) {}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.